-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
37 lines (28 loc) · 1006 Bytes
/
app.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
const express = require("express");
const app = express();
const mongo = require("mongodb");
const mongoose = require("mongoose");
const cors = require("cors");
require("dotenv").config();
const routes = require("./routes/routes");
// Body parser
app.use(express.urlencoded({ extended: true }));
// Cors used for FCC testing purposes
app.use(cors({ optionSuccessStatus: 200 }));
// Connection to the database and error handling
mongoose
.connect(process.env.DB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.catch((error) => console.log(error));
mongoose.connection.on("error", (error) => console.log(error));
// Serving styles and scripts from public dir
app.use(express.static("public"));
// Main route defined
app.get("/", (req, res) => res.sendFile(`${__dirname}/views/index.html`));
// Serving routes
app.use("/api/exercise", routes);
// Server listening
const port = process.env.PORT || 3000;
app.listen(port, () => console.log(`Server running at port ` + port));