-
Notifications
You must be signed in to change notification settings - Fork 120
/
Copy pathapp.js
68 lines (60 loc) · 1.87 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
import express from 'express';
import './mongodb/db.js';
import router from './routes/index.js';
import cookieParser from 'cookie-parser'
import session from 'express-session';
import connectMongo from 'connect-mongo';
import bodyParser from 'body-parser'
import multer from 'multer';
// import history from 'connect-history-api-fallback';
import config from './config'
import path from 'path';
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
let storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, './public/upload_imgs')
},
filename: function (req, file, cb) {
cb(null, Date.now() + file.originalname)
}
});
let upload = multer({storage: storage});
app.all('*', (req, res, next) => {
res.header("Access-Control-Allow-Origin", req.headers.origin || '*');
res.header("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
res.header("Access-Control-Allow-Credentials", true); //可以带cookies
res.header("X-Powered-By", '3.2.1');
res.header("Cache-Control", "public,max-age=60000");
if (req.method === 'OPTIONS') {
res.send(200);
} else {
next();
}
});
const MongoStore = connectMongo(session);
app.use(bodyParser.urlencoded({extended: false}))
app.use(bodyParser.json({}));
app.use(cookieParser());
app.use(session({
name: 'mt-session',
secret: 'meituan',
resave: true,
saveUninitialized: false,
cookie: {
httpOnly: true,
secure: false,
maxAge: 365 * 24 * 60 * 60 * 1000,
},
store: new MongoStore({
url: config.sessionStorageURL
})
}));
router(app);
// app.use(history());
console.log('*********************************')
console.log(`service start on ${config.port}`)
console.log('*********************************')
app.listen(config.port);
module.exports = app;