generated from imlinhanchao/vue-parcel-template
-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
70 lines (59 loc) · 2.06 KB
/
app.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
let express = require('express');
let fs = require('fs');
let path = require('path');
let favicon = require('serve-favicon');
let logger = require('morgan');
let cookieParser = require('cookie-parser');
let bodyParser = require('body-parser');
let session = require('express-session');
let robots = require('express-robots');
let FileStore = require('session-file-store')(session);
let partials = require('express-partials');
let files = require('./lib/files');
if (!fs.existsSync(path.join(__dirname, 'config.json'))) {
console.info('[Info] Please execute `npm run init` to initialization config.')
process.exit(0);
}
let api = require('./routes/api');
let view = require('./routes/view');
let config = require('./config');
let app = express();
files.mkdir(path.join(__dirname, 'public'));
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
app.use(robots({ UserAgent: '*', Disallow: '/' }));
app.use(favicon(path.join(__dirname, 'frontend', 'assets', 'favicon.ico')));
app.use(partials());
app.use(logger('dev'));
app.use(bodyParser.json({ limit: '2048kb' }));
app.use(bodyParser.urlencoded({ limit: '2048kb', extended: true }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
name: config.base.identityKey,
secret: config.base.secret,
store: new FileStore(),
saveUninitialized: false,
resave: false,
cookie: {
maxAge: 60 * 60 * 24 * 1000 * 365 // 有效期,单位是毫秒
}
}));
app.use('/api', api);
app.use('/view', view);
// 其他页面交给vue处理
app.use(function (req, res/*, next*/) {
res.sendFile(path.join(__dirname, 'public/index.html'));
});
// error handler
app.use(function (err, req, res) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};
// render the error page
res.status(err.status || 500);
res.render('error');
});
module.exports = app;