-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
54 lines (44 loc) · 1.32 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
var express = require('express')
var path = require('path')
var cookieParser = require('cookie-parser')
var logger = require('morgan')
const jwt = require('jsonwebtoken')
const { RunConfig } = require('./config/config')
var indexRouter = require('./routes/index')
var urlsRouter = require('./routes/urls')
var statisticsRouter = require('./routes/statistics')
var usersRouter = require('./routes/users')
const cors = require('cors')
var app = express()
// const corsOptions = {
// origin: 'http://127.0.0.1:5173',
// }
app.use(cors())
app.use(logger('dev'))
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
app.use(cookieParser())
app.use((req, res, next) => {
const token = req.header('Authorization')?.split(' ')[1]
if (!token) {
next()
return
}
jwt.verify(token, RunConfig.jwtSecret, (err, decoded) => {
if (err) {
return res.status(401).json({ message: 'Invalid token' })
}
req.user = decoded
})
next()
})
app.use('/md', express.static(path.join(__dirname, 'docs/md')))
app.use(express.static(path.join(__dirname, 'front/dist')))
app.get('/f/*', (req, res) => {
res.sendFile(path.join(__dirname, 'front/dist/index.html'))
})
app.use('/s', indexRouter)
app.use('/urls', urlsRouter)
app.use('/statistics', statisticsRouter)
app.use('/users', usersRouter)
module.exports = app