server.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. var express = require('express');
  2. // chenshilong add this, 2017.03.07 --------------------------------begin
  3. var path = require('path');
  4. var session = require('express-session');
  5. var DBStore = require('connect-mongo')(session);
  6. // -----------------------------------------------------------------end
  7. var app = express();
  8. var _rootDir = __dirname;
  9. app.use(express.static(_rootDir));
  10. // chenshilong add this, 2017.03.07 --------------------------------begin
  11. app.set('views', path.join(__dirname, 'web'));
  12. app.engine('.html', require('ejs').__express);
  13. app.set('view engine', 'html');
  14. // -----------------------------------------------------------------end
  15. var bodyParser = require('body-parser');
  16. app.use(bodyParser.urlencoded({extended: false}));
  17. app.use(bodyParser.json());
  18. // chenshilong add this, 2017.03.07 --------------------------------begin
  19. app.use(session({
  20. name: 'usersSession',
  21. secret: 'session users secret',
  22. cookie: {maxAge: 1000*60*30},
  23. resave: false,
  24. saveUninitialized: true,
  25. store: new DBStore({
  26. url: 'mongodb://localhost:27017/usersManages'
  27. })
  28. }));
  29. var users = require('./modules/users/routes/users_route');
  30. app.use('/', users);
  31. // -----------------------------------------------------------------end
  32. var apiRouter = express.Router();
  33. app.use('/api', apiRouter);
  34. app.use(function(req, res, next) {
  35. res.status(404).sendFile(_rootDir+'/src/404.html');
  36. });
  37. app.use(function(err, req, res, next) {
  38. console.error(err.stack);
  39. res.status(500).send('500 Error');
  40. });
  41. app.listen(6060);