-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (48 loc) · 1.59 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
51
52
53
54
55
56
57
58
59
60
61
// This is the Web Server
require('dotenv').config();
const express = require("express");
const server = express();
// enable cross-origin resource sharing to proxy api requests
// from localhost:3000 to localhost:4000 in local dev env
const cors = require("cors");
server.use(cors());
// create logs for everything
const morgan = require("morgan");
server.use(morgan("dev"));
// handle application/json requests
server.use(express.json());
// here's our static files
const path = require("path");
server.use(express.static(path.join(__dirname, "build")));
// here's our API
//todo
server.use("/api", require("./api"));
// 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");
// connect to the server
const PORT = process.env.PORT || 4000;
server.use((error, req, res, next) => {
res.send(error);
});
server.use((req, res, next) => {
res.status(404).json({ message: "Not Found"});
});
server.use((err, req, res, next) => {
res.status(500).json({ message: "Internal Server Error", err})
})
// define a server handle to close open tcp connection after unit tests have run
const handle = 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);
}
});
// export server and handle for routes/*.test.js
module.exports = { server, handle };