-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (42 loc) · 1.36 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
45
46
47
48
49
50
51
import express from 'express';
import mongoose from 'mongoose';
import todoRouter from './Routes/todos/index.js';
import bodyParser from 'body-parser';
import {DATABASEURL,PORT} from './config.js';
import { rateLimit } from "express-rate-limit";
const app = express();
const url = DATABASEURL || "mongodb://localhost:27017/tododb";
mongoose.connect(url, { useNewUrlParser: true });
const con = mongoose.connection;
try {
con.on('open', () => {
console.log('Connected to the database');
})
} catch (error) {
console.log("Error: " + error);
}
app.use((req, res, next) => {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader(
'Access-Control-Allow-Headers',
'Origin, X-Requested-With, Content-Type, Accept, Authorization'
);
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PATCH, DELETE');
next();
});
// limit incoming request from same IP
const limiter = rateLimit({
windowMs: 60 * 60 * 1000, // 1 hour window
max: 100, // start blocking after 100 requests
});
app.use(limiter); // apply to all requests
app.use(express.json());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use('/todos', todoRouter);
app.get('/', (req, res) => {
res.send('Welcome to the Todo App');
});
app.listen(PORT, () => {
console.log('Server started on port ' + PORT);
});