app.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. const express = require('express');
  2. const bodyParser = require('body-parser');
  3. const session = require('express-session');
  4. const path = require('path');
  5. const app = express();
  6. const captcha = require('./routes/captcha');
  7. const signup = require('./routes/signup');
  8. const signin = require('./routes/signin');
  9. const signout = require('./routes/signout');
  10. const index = require('./routes/index');
  11. const userList = require('./routes/userList');
  12. const databaseList = require('./routes/databaseList');
  13. //解决跨域问题
  14. app.all('*', (req, res, next) => {
  15. res.header("Access-Control-Allow-Origin", req.headers.origin); //设置来源
  16. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  17. res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  18. res.header("Access-Control-Max-Age", "604800000");
  19. res.header("Access-Control-Allow-Credentials", true);
  20. res.header("X-Powered-By", ' 3.2.1');
  21. // res.header("Content-Type", "application/json;charset=utf-8");
  22. next();
  23. });
  24. app.use(bodyParser.json());
  25. app.use(bodyParser.urlencoded({ extended: false }));
  26. //设置并使用session中间件
  27. app.use(session({
  28. secret: 'Nobody will know this secret shit, else me and the God',
  29. cookie: {
  30. maxAge: 1000 * 60 * 60 * 24 * 7
  31. },
  32. saveUninitialized: true,
  33. name: 'CSA_Uid'
  34. }));
  35. app.use(express.static(path.join(__dirname, 'public')));
  36. app.use('/v1/captcha', captcha);
  37. app.use('/v1/signup', signup);
  38. app.use('/v1/signin', signin);
  39. app.use('/v1/signout', signout);
  40. app.use('/v1/index', index);
  41. app.use('/v1/index/userList', userList);
  42. app.use('/v1/index/databaseList', databaseList);
  43. app.listen(3000);