-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
93 lines (79 loc) · 2.22 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import 'dotenv/config'
import 'express-async-errors'
import * as https from 'https'
import * as fs from 'fs'
// SERVER
import express from 'express'
const app = express()
// MIDDLEWARE
import cookieParser from 'cookie-parser'
app.use(express.json())
app.use(cookieParser())
app.use(express.static('static'))
// SECURITY
import helmet from 'helmet'
import cors from 'cors'
import xss from 'xss-clean'
import rateLimit from 'express-rate-limit'
app.use(helmet())
app.use(cors({
origin: [
'http://localhost:3000',
'http://localhost:9000',
'http://79.143.29.232:9000',
'https://psy-forum-sno.ru:9000'
],
credentials: true
}))
app.use(xss())
// app.use(rateLimit())
// ROUTES
import usersRouter from './routes/users.js'
import pointsRouter from './routes/points.js'
import authRouter from './routes/auth.js'
import achievementsRouter from './routes/achievements.js'
import imagesRouter from './routes/images.js'
import authMiddleware from './middleware/auth.js'
app.use('/api/v1/users', authMiddleware, usersRouter)
app.use('/api/v1/points', authMiddleware, pointsRouter)
app.use('/api/v1/achievements', authMiddleware, achievementsRouter)
app.use('/api/v1/images', authMiddleware, imagesRouter)
app.use('/api/v1/auth', authRouter)
// NOT EXISTING ROUTE
import notFoundMiddleware from './middleware/notFound.js'
app.use(notFoundMiddleware)
// ERROR HANDLERS
import errorHandlerMiddleware from './middleware/errorHandler.js'
app.use(errorHandlerMiddleware)
// RUNNING
import connectDb from './db/connect.js'
const port = process.env.PORT
const mongoUri = process.env.MONGO_URI
let isHttps = false
let key, cert
try {
key = fs.readFileSync('/etc/letsencrypt/live/psy-forum-sno.ru/privkey.pem')
cert = fs.readFileSync('/etc/letsencrypt/live/psy-forum-sno.ru/fullchain.pem')
isHttps = true
} catch (error) {
console.log(error)
}
let server = app
if (isHttps) {
console.log('https')
server = https.createServer({
key,
cert
}, app)
}
const start = async () => {
try {
await connectDb(mongoUri)
server.listen(port, () => {
console.log(`Server is listening on port ${port}`)
})
} catch (error) {
console.log(error)
}
}
start()