...
|
...
|
@@ -7,7 +7,6 @@ |
|
|
|
|
|
const config = require('./config/common');
|
|
|
|
|
|
|
|
|
// use one apm
|
|
|
if (config.useOneapm) {
|
|
|
require('oneapm');
|
...
|
...
|
@@ -20,16 +19,12 @@ const cookieParser = require('cookie-parser'); |
|
|
const favicon = require('serve-favicon');
|
|
|
const session = require('express-session');
|
|
|
const memcached = require('connect-memcached');
|
|
|
const _ = require('lodash');
|
|
|
const hbs = require('express-handlebars');
|
|
|
const pkg = require('./package.json');
|
|
|
const cookie = require('./library/cookie');
|
|
|
|
|
|
const app = express();
|
|
|
const MemcachedStore = memcached(session);
|
|
|
|
|
|
const setChannel = require('./doraemon/middleware/set-channel');
|
|
|
|
|
|
|
|
|
// 向模板注入变量
|
|
|
app.locals.devEnv = app.get('env') === 'development';
|
|
|
app.locals.version = pkg.version;
|
...
|
...
|
@@ -37,8 +32,15 @@ app.locals.version = pkg.version; |
|
|
// 指定libray目录
|
|
|
global.library = path.resolve('./library/');
|
|
|
|
|
|
|
|
|
app.set('view engine', '.hbs');
|
|
|
app.set('views', './doraemon/views');
|
|
|
app.engine('.hbs', hbs({
|
|
|
extname: '.hbs',
|
|
|
defaultLayout: 'layout',
|
|
|
layoutsDir: './doraemon/views',
|
|
|
partialsDir: './doraemon/views/partial',
|
|
|
helpers: require(`${global.library}/helpers`)
|
|
|
}));
|
|
|
|
|
|
app.use(favicon(path.join(__dirname, '/public/favicon.ico')));
|
|
|
app.use(express.static(path.join(__dirname, 'public')));
|
...
|
...
|
@@ -65,35 +67,35 @@ app.use(session({ |
|
|
})
|
|
|
}));
|
|
|
|
|
|
|
|
|
// req和res绑定yoho对象,用于传递全局数据, 如req.yoho.channel等
|
|
|
app.use((req, res, next) => {
|
|
|
req.yoho = {};
|
|
|
res.yoho = {};
|
|
|
next();
|
|
|
});
|
|
|
req.user = {}; // 全局的用户数据
|
|
|
req.yoho = {}; // req和res绑定yoho对象,用于传递全局数据, 如req.yoho.channel等
|
|
|
|
|
|
app.use((req, res, next) => {
|
|
|
req.user = {};
|
|
|
|
|
|
// 从 PHP 写的 SESSION 中获取到当前登录用户的 UID
|
|
|
if (req.session && _.isNumber(req.session._LOGIN_UID)) {
|
|
|
req.user.uid = req.session._LOGIN_UID;
|
|
|
}
|
|
|
|
|
|
// session 没有读取到的时候,从 cookie 读取 UID
|
|
|
if (!req.user.uid && req.cookies._UID) {
|
|
|
req.user.uid = cookie.getUid(req);
|
|
|
}
|
|
|
next();
|
|
|
});
|
|
|
|
|
|
const logger = require('./library/logger');
|
|
|
const user = require('./doraemon/middleware/user');
|
|
|
const setChannel = require('./doraemon/middleware/set-channel');
|
|
|
const errorHanlder = require('./doraemon/middleware/error-hanlder');
|
|
|
|
|
|
// YOHO 前置中间件
|
|
|
app.use(user());
|
|
|
app.use(setChannel());
|
|
|
|
|
|
// dispatcher
|
|
|
require('./dispatch')(app);
|
|
|
try {
|
|
|
require('./dispatch')(app);
|
|
|
|
|
|
app.all('*', errorHanlder.notFound()); // 404
|
|
|
|
|
|
// YOHO 后置中间件
|
|
|
app.use(errorHanlder.serverError());
|
|
|
} catch (err) {
|
|
|
logger.error(err);
|
|
|
}
|
|
|
|
|
|
// listener
|
|
|
app.listen(config.port, function() {
|
|
|
console.log('yohobuy start');
|
|
|
logger.info('yohobuy start');
|
|
|
}); |
...
|
...
|
|