|
@@ -1,32 +1,53 @@
|
|
|
-var createError = require('http-errors');
|
|
|
-var express = require('express');
|
|
|
-var path = require('path');
|
|
|
-var cookieParser = require('cookie-parser');
|
|
|
-var logger = require('morgan');
|
|
|
+const createError = require('http-errors');
|
|
|
+const express = require('express');
|
|
|
+const path = require('path');
|
|
|
+const cookieParser = require('cookie-parser');
|
|
|
+const session = require('express-session');
|
|
|
+const ejs = require('ejs');
|
|
|
+const bodyParser = require('body-parser');
|
|
|
|
|
|
-var indexRouter = require('./routes/index');
|
|
|
-var usersRouter = require('./routes/users');
|
|
|
|
|
|
-var app = express();
|
|
|
+const indexRouter = require('./routes/index');
|
|
|
+const usersRouter = require('./routes/users');
|
|
|
+
|
|
|
+const app = express();
|
|
|
+
|
|
|
+const serverConfig = require('./configs/server.json');
|
|
|
|
|
|
// view engine setup
|
|
|
app.set('views', path.join(__dirname, 'views'));
|
|
|
app.set('view engine', 'ejs');
|
|
|
|
|
|
-app.use(logger('dev'));
|
|
|
-app.use(express.json());
|
|
|
-app.use(express.urlencoded({ extended: false }));
|
|
|
-app.use(cookieParser());
|
|
|
-app.use(express.static(path.join(__dirname, 'public')));
|
|
|
+app.use('/js', express.static(path.join(__dirname, './public/js')));
|
|
|
+app.use('/js', express.static(path.join(__dirname, './public/img')));
|
|
|
+app.use('/', express.static(path.join(__dirname, './public')));
|
|
|
+
|
|
|
+// 配置session
|
|
|
+app.use(
|
|
|
+ session({
|
|
|
+ secret: 'air',
|
|
|
+ name: 'session', //这里的name值得是cookie的name,默认cookie的name是:connect.sid
|
|
|
+ cookie: { maxAge: 1800000 }, //过期时间半小时
|
|
|
+ keys: ['owner', 'captcha'], // 用户登陆信息,验证码字段
|
|
|
+ resave: true,
|
|
|
+ saveUninitialized: true,
|
|
|
+ })
|
|
|
+);
|
|
|
+
|
|
|
+app.use(bodyParser.urlencoded({ limit: '10mb', extended: true }));
|
|
|
+app.use(bodyParser.json({ limit: '10mb' }));
|
|
|
+
|
|
|
|
|
|
app.use('/', indexRouter);
|
|
|
app.use('/users', usersRouter);
|
|
|
|
|
|
+
|
|
|
// catch 404 and forward to error handler
|
|
|
app.use(function(req, res, next) {
|
|
|
next(createError(404));
|
|
|
});
|
|
|
|
|
|
+
|
|
|
// error handler
|
|
|
app.use(function(err, req, res, next) {
|
|
|
// set locals, only providing error in development
|
|
@@ -38,4 +59,7 @@ app.use(function(err, req, res, next) {
|
|
|
res.render('error');
|
|
|
});
|
|
|
|
|
|
+app.listen(serverConfig.port, serverConfig.host, ()=>{
|
|
|
+ log.warn(`服务启动 server is running to @http://${serverConfig.host}:${serverConfig.port}`);
|
|
|
+})
|
|
|
module.exports = app;
|