app.js 2.1 KB
/**
 * app.js
 * @author: feng.chen<feng.chen@yoho.cn>
 * @date: 2017/04/13
 */
'use strict';

const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
const compression = require('compression');
const express = require('express');
const pkg = require('../package.json');

// const session = require('express-session');
const cookieSession = require('cookie-session');
const favicon = require('serve-favicon');
const path = require('path');
const multipart = require('connect-multiparty');
const multipartMiddleware = multipart();

global.env = {
  version: pkg.version,
  Production: process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'gray',
  Gray: process.env.NODE_ENV === 'gray',
  Test: (process.env.NODE_ENV || '').indexOf('test') >= 0,
};

const app = express();

const config = require('./common/config');
const logger = require('yoho-node-lib/lib/logger').init(config);
const helpers = require('yoho-node-lib/lib/helpers');

global.yoho = {
  logger,
  helpers,
  config,
};
app.use(
  cookieSession({
    name: 'yoho-shop',
    secret: 'yoho!shop@manage',
    maxAge: 2 * 60 * 60 * 1000,
  })
);

app.use(compression());
app.use(favicon(path.join(__dirname, '/favicon.ico')));
app.use(express.static(path.join(__dirname, 'public')));
app.use(
  bodyParser.json({
    limit: 1 * 1024 * 1024, // 限制请求长度1m
  })
);
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(multipartMiddleware);

const middleware = require('./middleware');
const controllers = require('./controllers');

app.use('/crm/common/ok.jsp', (req, res) => {
  res.status(200).end();
});
app.head('*', (req, res) => {
  res.status(200).end();
});

try {
  app.use(middleware.main);

  // 前置中间件
  app.use(middleware.before);

  // controller
  app.use('/Api', controllers);

  // 鉴权中间件
  app.use(middleware.auth);

  // 接口代理中间件
  app.use(middleware.proxy);

  // 异常捕获中间件
  app.use(middleware.error);
} catch (err) {
  logger.error(err);
}

app.listen(config.port, () => {
  logger.info(`yoho shop manage start at ${config.port}`);
});