-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
73 lines (51 loc) · 1.56 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
// Basic Lib Import
const express = require('express')
const router = require('./src/routes/api');
const app = new express();
const bodyParser = require('body-parser');
const dotenv = require('dotenv');
// env implementation
dotenv.config({path: "./config.env"});
// Security middleware Lib Import
const rateLimit = require('express-rate-limit');
const helmet = require('helmet');
const mongoSanitize = require('express-mongo-sanitize');
const xss = require('xss-clean');
const hpp = require('hpp');
const cors = require('cors');
// Database Lib Import
const mongoose = require('mongoose');
const path = require('path');
// Security Middleware Implement
app.use(cors())
app.use(helmet())
app.use(mongoSanitize())
app.use(xss())
app.use(hpp())
// Body Parser Implement
app.use(bodyParser.json());
// Request Rate Limit
const limiter = rateLimit({windowMs: 15*60*1000, max: 3000})
app.use(limiter)
// Mongo DB Database Connection
let URI = 'mongodb://127.0.0.1:27017/NewTodo';
let OPTION = {user: '', pass: ''}
mongoose.connect(URI, OPTION, error => {
if (error){
console.log('Database connection fail, the error is: ' + error)
}else{
console.log('DB Connection Success')
}
})
// Routing Implement
app.use('/api/v1', router);
//Tagging frontend
app.use(express.static('client/build'));
app.use('*', (req, res) => {
req.sendFile(path.resolve(__dirname, 'client', 'build', 'index.html'));
});
// Undefined Route Implement
// app.use('*', (req, res) => {
// res.status(404)/json({ status: 'fail', data: 'Not Found'});
// });
module.exports = app;