-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
59 lines (48 loc) · 1.7 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
55
56
57
58
59
//require system
const express = require('express');
const mongoose = require('mongoose');
const {consola} = require('consola');
const cors = require('cors');
const serve = require('express-static');
const path = require('path');
const favicon = require('serve-favicon');
const morgan = require('morgan');
const {expressjwt} = require('express-jwt');
const crypto = require('crypto');
// consola.box(crypto.randomBytes(64).toString('hex'));//then put to .env file
require('dotenv').config();
//require self
const {errorHandle} = require('./middleware');
const {rootRouter,apiRouter,loginRouter,registRouter} = require('./routers');
const db = require('./dbs');
//variable
const port = 3000;
const app = express();
//db
mongoose.connect(db.address).then(()=>{
consola.success('连接数据库成功!!');
}).catch((e)=>{
consola.error('连接数据库出错');
consola.error(e);
});
//middlewire
app.use(morgan('dev'));
// app.use(bodyParser.json());//不用再安装body-parser了,node4.16.0以后就集成了body-parser了
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cors());
app.use(favicon(path.join(__dirname, '/static', 'favicon.ico')))
app.use('/static',serve(__dirname + '/static'));
app.use(//expressjwt中间件已经做了jwt验证工作,所以jsonwebtoken的verify函数就不用我们自己调用了
expressjwt({
secret: process.env.SECRET_KEY,
algorithms: ["HS256"],
}).unless({ path: ['/',"/login","/regist"] })
);
app.use('/',rootRouter);
app.use('/api',apiRouter);
app.use('/login',loginRouter);
app.use('/regist',registRouter);
app.use(errorHandle);
//listen
app.listen(port,()=>{consola.log(`connect the http://localhost:${port}`);});