-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (35 loc) · 1.03 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
import express from 'express';
import cors from 'cors';
import { readdirSync } from 'fs';
import mongoose from 'mongoose';
import csrf from 'csurf';
import cookieParser from 'cookie-parser';
const morgan = require('morgan');
require('dotenv').config();
const csrfProtection = csrf({ cookie: true });
// Express app initialized
const app = express();
// Database
mongoose
.connect(process.env.DATABASE)
.then(() => console.log('**Database connected!**'))
.catch((err) =>
console.log('Database connection error! Check server end-point. ', err)
);
// Middleware
app.use(cors());
app.use(express.json({ limit: '5mb' }));
app.use(cookieParser());
app.use(morgan('dev'));
// Routes auto-importing middleware
readdirSync('./routes').map((r) => {
app.use('/api', require(`./routes/${r}`));
});
// CSRF
app.use(csrfProtection);
app.get('/api/csrf-token', (req, res) => {
res.json({ csrfToken: req.csrfToken() });
});
// Port
const port = process.env.PORT || 8000;
app.listen(port, () => console.log(`Server is running on PORT ${port}`));