app.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 chalk from 'chalk';
  13. // import Statistic from './middlewares/statistic'
  14. const app = express();
  15. app.all('*', (req, res, next) => {
  16. res.header("Access-Control-Allow-Origin", req.headers.origin || req.headers.referer || '*');
  17. res.header("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With");
  18. res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE");
  19. res.header("Access-Control-Allow-Credentials", true); //可以带cookies
  20. res.header("X-Powered-By", 'Express');
  21. next();
  22. });
  23. // app.use(Statistic.apiRecord)
  24. const MongoStore = connectMongo(session);
  25. app.use(cookieParser());
  26. app.use(session({
  27. name: config.session.name,
  28. secret: config.session.secret,
  29. resave: true,
  30. saveUninitialized: false,
  31. cookie: config.session.cookie,
  32. store: new MongoStore({
  33. url: config.url
  34. })
  35. }))
  36. // app.use(expressWinston.logger({
  37. // transports: [
  38. // new (winston.transports.Console)({
  39. // json: true,
  40. // colorize: true
  41. // }),
  42. // new winston.transports.File({
  43. // filename: 'logs/success.log'
  44. // })
  45. // ]
  46. // }));
  47. router(app);
  48. // app.use(expressWinston.errorLogger({
  49. // transports: [
  50. // new winston.transports.Console({
  51. // json: true,
  52. // colorize: true
  53. // }),
  54. // new winston.transports.File({
  55. // filename: 'logs/error.log'
  56. // })
  57. // ]
  58. // }));
  59. app.use(history());
  60. app.use(express.static('./public'));
  61. app.listen(config.port, () => {
  62. console.log(
  63. chalk.green(`成功监听端口:${config.port}`)
  64. )
  65. });