app.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. import express from 'express';
  2. import db from './mongodb/db.js';
  3. import config from 'config-lite';
  4. import router from './routes/index.js';
  5. import cookieParser from 'cookie-parser'
  6. import session from 'express-session';
  7. import connectMongo from 'connect-mongo';
  8. import winston from 'winston';
  9. import expressWinston from 'express-winston';
  10. import path from 'path';
  11. import history from 'connect-history-api-fallback';
  12. // import Statistic from './middlewares/statistic'
  13. const app = express();
  14. app.all('*', (req, res, next) => {
  15. res.header("Access-Control-Allow-Origin", req.headers.origin || '*');
  16. res.header("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With");
  17. res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  18. res.header("Access-Control-Allow-Credentials", true); //可以带cookies
  19. res.header("X-Powered-By", '3.2.1')
  20. if (req.method == 'OPTIONS') {
  21. res.send(200);
  22. } else {
  23. next();
  24. }
  25. });
  26. // app.use(Statistic.apiRecord)
  27. const MongoStore = connectMongo(session);
  28. app.use(cookieParser());
  29. app.use(session({
  30. name: config.session.name,
  31. secret: config.session.secret,
  32. resave: true,
  33. saveUninitialized: false,
  34. cookie: config.session.cookie,
  35. store: new MongoStore({
  36. url: config.url
  37. })
  38. }))
  39. // app.use(expressWinston.logger({
  40. // transports: [
  41. // new (winston.transports.Console)({
  42. // json: true,
  43. // colorize: true
  44. // }),
  45. // new winston.transports.File({
  46. // filename: 'logs/success.log'
  47. // })
  48. // ]
  49. // }));
  50. router(app);
  51. // app.use(expressWinston.errorLogger({
  52. // transports: [
  53. // new winston.transports.Console({
  54. // json: true,
  55. // colorize: true
  56. // }),
  57. // new winston.transports.File({
  58. // filename: 'logs/error.log'
  59. // })
  60. // ]
  61. // }));
  62. app.use(history());
  63. app.use(express.static('./public'));
  64. app.listen(config.port);