-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserver.js
41 lines (31 loc) · 974 Bytes
/
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
// Imports
const express = require("express");
const cors = require("cors");
const bodyParser = require("body-parser");
const dotenv = require("dotenv");
const db = require("./database/database");
const exerciseRouter = require("./routes/exercise");
// Basic configuration, Environment variables and DB connection
dotenv.config();
db.connect();
const app = express()
app.use(cors({ optionsSuccessStatus: 200 }));
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(bodyParser.json());
// Static files
app.use(express.static("public"));
// Routing
app.get("/", (req, res) => {
res.status(200).sendFile(`${__dirname}/views/index.html`);
});
app.use("/api/exercise", exerciseRouter);
// No matching route
app.use(function(req, res) {
res.status(404).sendFile(`${__dirname}/views/404.html`);
});
// Server listening
const listener = app.listen(process.env.PORT || 3000, () => {
console.log("App is listening on port " + listener.address().port)
})