-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
40 lines (34 loc) · 1.01 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
const path = require("path");
//my tools for getting and proccessing data
const scrape = require("./src/utils/data-scrapper");
const sort = require("./src/utils/sort-lectures");
//express
const express = require(`express`);
const cors = require("cors");
//for env file
require("dotenv").config();
//setting up app
const app = express();
const port = process.env.PORT || 5000;
//middleware
app.use(cors());
app.use(express.json());
app.get("/courses/:id", async (req, res) => {
const courseNumber = req.params.id;
const data = await scrape(courseNumber);
data.length == 0
? res.status(404).send({ error: "wrong number" })
: res.send(sort(data));
});
//server static assets if in production:
if (process.env.NODE_ENV === "production") {
//set static folder
app.use(express.static("client/build"));
app.get("*", (req, res) => {
res.sendFile(path.resolve("client/build/index.html"));
});
}
const server = app.listen(port, () => {
console.log(`server is up on port ` + port);
});
server.setTimeout(900000);