-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
44 lines (36 loc) · 1.2 KB
/
Copy pathapp.js
File metadata and controls
44 lines (36 loc) · 1.2 KB
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
const bodyParser = require("body-parser");
const rateLimit = require("express-rate-limit");
const cors = require('cors');
const express = require('express');
const app = express();
const mongooseConnection = require('./db');
const users = require("./routes/api/users");
// Config from Environment variables
let keys = {};
keys.clientUrl = process.env.APP_CLIENTURL;
/**
* Index.js does the following:
* 1. Sets up middleware for Rate Limiting & Body Parser (used for JSON).
* 2. Connects to the MongoDB for CRUD operations.
* 3. Express server to serve API endpoints & /resetpassword webpage
* 4. Connects socket.io to Express server to enable realtime
* bidirectional communication for chat messaging functionality.
*/
// Enable CORS
app.use(cors({
origin:['http://localhost:3000', keys.clientUrl],
methods:['GET','POST'],
}));
// Rate Limiter Middleware
const limiter = rateLimit({
windowMs: 15 * 60 * 1000, // 15 minutes
max: 100 // limit each IP to 100 requests per windowMs
});
// apply limiter to all requests
app.use(limiter);
// Bodyparser middleware
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
// Routes
app.use("/api/users", users);
module.exports = app;