-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
40 lines (33 loc) · 1.13 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
require('dotenv').config();
const express = require('express');
const mongoose = require('mongoose');
const cookieParser = require('cookie-parser');
const helmet = require('helmet');
const { errors } = require('celebrate');
const { CORS } = require('./src/middlewares/CORS');
const { NODE_ENV, mongoDB } = process.env;
const { mongo } = require('./src/utils/config');
const { requestLogger, errorLogger } = require('./src/middlewares/logger');
const { limiter } = require('./src/middlewares/rateLimit');
const errorProcessing = require('./src/middlewares/errorProcessing');
const router = require('./src/routes/index');
const app = express();
app.use(CORS);
app.use(requestLogger);
app.use(limiter);
app.use(express.json());
app.use(cookieParser());
app.use(helmet());
const { PORT = 3001 } = process.env;
mongoose.connect(NODE_ENV === 'production' ? mongoDB : mongo);
app.use((req, res, next) => {
console.log(`${req.method}: ${req.path} ${JSON.stringify(req.body)}`);
next();
});
app.use('/api', router);
app.use(errorLogger);
app.use(errors());
app.use(errorProcessing);
app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
});