-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
61 lines (44 loc) · 1.68 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
53
54
55
56
57
58
59
60
// require express, cors, mongoose, dotenv.
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
require("dotenv").config();
const rateLimit = require("express-rate-limit");
// Create app, port variables.
const app = express();
const port = process.env.PORT || 5000;
// Declare how many requests can be done in an hour.
const limiter = rateLimit({
windowMS: 15 * 60 * 1000,
max: 20,
message: "Too many requests to handle at this hour!"
});
// Trust proxy for heroku.
app.set("trust proxy", 1);
// Use the limiter
app.use(limiter);
// App .use cors so we can run server on localhost, passing the restrictions.
app.use(cors());
// And express.json(), to return info in JSON.
app.use(express.json());
// URI FOR MongoDB
const uri = process.env.MONGODB_URI;
// Connecting to mongoose, uri
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true });
// Creating a connection instance variable.
const connection = mongoose.connection;
// Once the connection is "open" => log -||-
connection.once('open', () => {
console.log("MongoDB database connection established successfully");
});
// Routes for the server to use;
// The server URL is https://localhost:5000.
// Now if you add “/exercises” or “/users” on the end it will load the endpoints defined in the corresponding router files.
const exercisesRouter = require("./routes/exercises.route");
// const usersRouter = require("./routes/users.route");
app.use("/exercises", limiter, exercisesRouter);
// app.use("/users", usersRouter);
// Makes the local server run.
app.listen(port, () => {
console.log(`Server is running on port: ${port}`);
});