-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
52 lines (40 loc) · 1.41 KB
/
server.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
const express = require('express');
const bodyParser = require('body-parser');
const RateLimit = require('express-rate-limit');
// create express app
const app = express();
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: true }))
// parse application/json
app.use(bodyParser.json())
// Configuring the database
const dbConfig = require('./config/database.config.js');
const mongoose = require('mongoose');
mongoose.Promise = global.Promise;
// Connecting to the database
mongoose.connect(dbConfig.url, {
useNewUrlParser: true
}).then(() => {
console.log("Successfully connected to the database");
}).catch(err => {
console.log('Could not connect to the database. Exiting now...', err);
process.exit();
});
// Preventing DDOS and Brute-Force attacks
app.enable('trust proxy');
// Setting maximum Limit server entertains in a particular window.
var limiter = new RateLimit({
windowMs: 2 * 60 * 1000, // 2 minutes
max: 1000, // limit each IP to 1000 requests per windowMs
delayMs: 0 // disable delaying - full speed until the max limit is reached
});
app.use(limiter);
// define a simple route
app.get('/', (req, res) => {
res.json({ "message": "Welcome to Advertisement Server. Check routes.js for APIs." });
});
require('./app/routes/ads.routes.js')(app);
// listen for requests
app.listen(3000, () => {
console.log("Server is listening on port 3000");
});