app.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. const express = require('express');
  2. const path = require('path');
  3. const favicon = require('serve-favicon');
  4. const logger = require('morgan');
  5. const cookieParser = require('cookie-parser');
  6. const bodyParser = require('body-parser');
  7. const index = require('./routes/index');
  8. const users = require('./routes/users');
  9. const wechatpay = require('./routes/wechatpay');
  10. const app = express();
  11. //解决跨域问题
  12. app.all('*', (req, res, next) => {
  13. res.header("Access-Control-Allow-Origin", req.headers.origin); //设置来源
  14. res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  15. res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  16. res.header("Access-Control-Max-Age", "604800000");
  17. res.header("Access-Control-Allow-Credentials", true);
  18. res.header("X-Powered-By", ' 3.2.1');
  19. // res.header("Content-Type", "application/json;charset=utf-8");
  20. next();
  21. });
  22. // view engine setup
  23. app.set('views', path.join(__dirname, 'views'));
  24. app.set('view engine', 'jade');
  25. // uncomment after placing your favicon in /public
  26. //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico')));
  27. app.use(logger('dev'));
  28. app.use(bodyParser.json());
  29. app.use(bodyParser.urlencoded({ extended: false }));
  30. app.use(cookieParser());
  31. app.use(express.static(path.join(__dirname, 'public')));
  32. app.use('/', index);
  33. app.use('/users', users);
  34. app.use('/wechatpay', wechatpay)
  35. // catch 404 and forward to error handler
  36. app.use(function(req, res, next) {
  37. var err = new Error('Not Found');
  38. err.status = 404;
  39. next(err);
  40. });
  41. // error handler
  42. app.use(function(err, req, res, next) {
  43. // set locals, only providing error in development
  44. res.locals.message = err.message;
  45. res.locals.error = req.app.get('env') === 'development' ? err : {};
  46. // render the error page
  47. res.status(err.status || 500);
  48. res.render('error');
  49. });
  50. module.exports = app;