Authored by xuqi
node_modules/
.eslintcache
npm-debug.log
\ No newline at end of file
npm-debug.log
# Created by https://www.gitignore.io/api/node
### Node ###
# Logs
logs
*.log
npm-debug.log*
# Runtime data
pids
*.pid
*.seed
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# node-waf configuration
.lock-wscript
# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules
jspm_packages
# Optional npm cache directory
.npm
# Optional REPL history
.node_repl_history
.eslintcache
.jshintignore
.jshintrc
... ...
{
"extends": "stylelint-config-yoho",
"ignoreFiles": ["public/*/dist/"]
}
... ...
... ... @@ -17,21 +17,24 @@ var app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'hbs');
app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(favicon(path.join(__dirname, '/public/favicon.ico')));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
// app.use(function(req, res, next) {
// var err = new Error('Not Found');
// err.status = 404;
// next(err);
// });
app.use('/', require('./router'));
app.use(function(req, res) {
var err = new Error('Not Found');
err.status = 404;
res.send(404, err);
});
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
app.use(function(err, req, res) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
... ... @@ -40,14 +43,12 @@ if (app.get('env') === 'development') {
});
}
app.use(function(err, req, res, next) {
app.use(function(err, req, res) {
res.status(err.status || 500);
res.render('error', {
message: err.message,
error: {}
message: err.message,
error: {}
});
});
require('./router')(app);
module.exports = app;
... ...
... ... @@ -7,19 +7,45 @@
var rp = require('request-promise');
const API_URL = 'http://testapi.yoho.cn:28078/';
const DEV_API_URL = 'http://devapi.yoho.cn:58078/';
const SERVICE_URL = 'http://testservice.yoho.cn:28077/';
const YOHOBUY_URL = 'http://www.yohobuy.com/';
const API_OLD = 'http://test2.open.yohobuy.com/';
const API_URL = 'http://testapi.yoho.cn:28078';
// const DEV_API_URL = 'http://devapi.yoho.cn:58078';
// const SERVICE_URL = 'http://testservice.yoho.cn:28077';
// const YOHOBUY_URL = 'http://www.yohobuy.com';
// const API_OLD = 'http://test2.open.yohobuy.com';
class API {
constructor() {
this.headers = {
'User-Agent': 'YOHO WEB NODE' // TODO: 请求的服务端是否有要求格式,是否添加
};
this.timeout = 5000;
this.catchError = function(err) {
// TODO: 完善 API 调用错误日志记录
console.log('接口调用失败', err);
return Promise.reject(err);
};
}
get(url, data) {
return rp({
url: `${API_URL}${url}`,
data: data
});
headers: this.headers,
timeout: this.timeout,
qs: data
}).catch(this.catchError);
}
post(url, data) {
return rp({
url: `${API_URL}${url}`,
method: 'post',
headers: this.headers,
timeout: this.timeout,
form: data // TODO: post 请求格式不知后端要求 json 还是 form 提交,需要后续完善
}).catch(this.catchError);
}
}
module.exports = API;
\ No newline at end of file
module.exports = API;
... ...
... ... @@ -3,12 +3,15 @@
* @author: xuqi<qi.xu@yoho.cn>
* @date: 2016/3/29
*/
var API= require('../../library/api');
var API = require('../../library/api');
var api = new API();
module.exports = (cb) => {
// var lotteryPromise = api.get('', Object.assign({}, ...));
// lotteryPromise.then((data) => {
// cb(data);
// });
};
\ No newline at end of file
module.exports = (data) => {
// 接受控制器传来的参数,并进行校验处理, 等后续操作
// 处理完成后,发给后端
data = Object.assign({}, data);
// 数据返回一个 Promise
return api.get('/wx', data);
};
... ...
... ... @@ -4,7 +4,8 @@
"private": true,
"scripts": {
"start": "node ./bin/www",
"lint": "eslint -c .eslintrc --cache public/**/*.js"
"lint": "eslint -c .eslintrc --fix --cache public/**/*.js",
"validate": "npm ls"
},
"dependencies": {
"body-parser": "~1.13.2",
... ... @@ -43,9 +44,11 @@
"postcss-short": "^1.4.0",
"postcss-sprites": "^3.1.1",
"postcss-use": "^2.0.2",
"precommit-hook": "^3.0.0",
"precss": "^1.4.0",
"vinyl-named": "^1.1.0",
"webpack-stream": "^3.1.0"
"precommit-hook": "^3.0.0",
"webpack-stream": "^3.1.0",
"eslint-config-yoho": "^0.1.1",
"stylelint-config-yoho": "^1.1.0"
}
}
... ...
... ... @@ -4,11 +4,13 @@
* @date: 2016/3/28
*/
'use stricts';
const express = require('express');
const app = new express.Router();
const routes = './routes';
var wxLottery = require(`${routes}/wxLottery/index`);
module.exports = (app) => {
app.get('/wxLottery', wxLottery.index);
};
\ No newline at end of file
app.get('/wxLottery', wxLottery.index);
module.exports = app;
... ...
... ... @@ -5,13 +5,21 @@
*/
var lotteryModel = require('../../models/wxLottery/index');
var errUtil = require('../../util/error');
exports.index = (req, res, next) => {
// lotteryModel((data) => {
exports.index = (req, res) => {
// });
res.render('wxLottery/index', {
title: '微信抽奖',
// wxLottery: data
// 传入参数给lotteryModel发送到后端,获取返回的数据
lotteryModel({test: 1}).then((data) => {
// 接受接口返回到的数据,处理后,渲染页面或者返回 JSON
res.send(data);
// res.render('wxLottery/index', {
// title: '微信抽奖',
// wxLottery: data
// });
}).catch((err) => {
errUtil.webError(err, res);
});
};
\ No newline at end of file
};
... ...
// 控制返回到页面的错误信息
exports.webError = (err, res) => {
console.log(err);
// 单独放在组件
// 容错处理,根据环境不同,决定是否返回到前端
res.sendStatus(500);
};
... ...