-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
50 lines (40 loc) · 1.21 KB
/
index.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
// This is the Web Server
const express = require("express");
const server = express();
const cors = require("cors");
server.use(cors());
//process env variables
require("dotenv").config();
// create logs for everything
const morgan = require("morgan");
server.use(morgan("dev"));
// handle application/json requests
const bodyParser = require("body-parser");
server.use(bodyParser.json());
// here's our static files
const path = require("path");
server.use(express.static(path.join(__dirname, "build")));
// here's our API
server.use("/api", require("./routes"));
// by default serve up the react app if we don't recognize the route
server.use((req, res, next) => {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
// bring in the DB connection
const { client } = require("./db");
// error handler
server.use((err, req, res, next) => {
console.error(err);
res.status(500).send(err);
});
// connect to the server
const PORT = process.env.PORT || 5000;
server.listen(PORT, async () => {
console.log(`Server is running on ${PORT}!`);
try {
await client.connect();
console.log("Database is open for business!");
} catch (error) {
console.error("Database is closed for repairs!\n", error);
}
});