-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
54 lines (44 loc) · 1.34 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const path = require('path');
const express = require('express');
const session = require('express-session');
const MongoStore = require('connect-mongo')(session);
const flash = require('connect-flash');
const config = require('config-lite')(__dirname);
const routes = require('./routes');
const pkg = require('./package');
const app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
name: config.session.key,
secret: config.session.secret,
resave: true,
saveUninitialized: false,
cookie: {
maxAge: config.session.maxAge
},
store: new MongoStore({
url: config.mongodb
})
}));
app.use(flash());
//处理表单及文件上传的中间件
app.use(require('express-formidable')({
uploadDir: path.join(__dirname, 'public/img'), //上传文件路径
keepExtensions: true //保留后缀
}));
app.locals.blog = {
title: pkg.name,
description: pkg.description
};
app.use(function (req, res, next) {
res.locals.user = req.session.user;
res.locals.success = req.flash('success').toString();
res.locals.error = req.flash('error').toString();
next();
});
routes(app);
app.listen(config.port, function () {
console.log(`${pkg.name} listening on port ${config.port}`);
});