forked from goitacademy/nodejs-homework-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
39 lines (30 loc) · 1.02 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
const express = require('express')
const logger = require('morgan')
const cors = require('cors')
const helmet = require('helmet')
const rateLimit = require('express-rate-limit')
require('dotenv').config()
const {contactsRouter} = require('./routes/api')
const {authRouter} = require('./routes/api')
const app = express()
const formatsLogger = app.get('env') === 'development' ? 'dev' : 'short'
app.use(helmet())
app.use(logger(formatsLogger))
app.use(cors())
app.use(express.json({ limit: 10000 }))
app.use(express.static('public'))
app.use('/api/', rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100 // limit each IP to 100 requests per windowMs
}))
app.use('/api/contacts', contactsRouter)
app.use('/api/users', authRouter)
app.use('/api/users/current', contactsRouter)
app.use((req, res) => {
res.status(404).json({ message: 'Not found' })
})
app.use((err, req, res, next) => {
const status = err.status || 500
res.status(status).json({ status: 'fail', code: status, message: err.message })
})
module.exports = app