forked from hackforla/food-oasis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
89 lines (72 loc) · 2.49 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
const express = require("express");
const dotenv = require("dotenv");
// const massive = require("massive");
dotenv.config();
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");
const path = require("path");
const middleware = require("./middleware/middleware");
const router = require("./app/routes/index");
const app = express();
// Redirect HTTP requests to HTTPS
if (process.env.NODE_ENV === "production") {
app.use((req, res, next) => {
// This way of detecting insecure requests is specific
// to a Heroku deployment. May need modification for
// other production deployment platforms.
if (req.header("x-forwarded-proto") !== "https")
// redirect to https with same host & url
res.redirect(`https://${req.header("host")}${req.url}`);
else next();
});
}
app.use(middleware.cors);
// app.use(middleware.logger);
// Serve static files from the React app
app.use(express.static(path.join(__dirname, "client/build")));
app.use(express.static("public"));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(cookieParser());
// Connect to DB using massive
// massive(
// process.env.DATABASE_URL || {
// poolSize: 10,
// user: process.env.POSTGRES_USERNAME,
// host: process.env.POSTGRES_HOST,
// database: process.env.POSTGRES_DATABASE,
// password: process.env.POSTGRES_PASSWORD,
// port: Number(process.env.POSTGRES_PORT),
// ssl: { rejectUnauthorized: false },
// // ssl: process.env.POSTGRES_SSL === "true",
// }
// )
// .then((database) => {
// app.set("db", database);
// console.log("database connected!");
// })
// .catch((err) => {
// console.log(err);
// });
app.use(router);
// The following three routes are for testing purposes, and may be deleted later.
app.get("/hello/:name", (req, res) => {
res.status(200).json({ hello: req.params.name });
});
app.get("/throw", (req, res, next) => {
next(new Error("Ouch!"));
//throw new Error("Test Exception Handler");
});
app.get("/health", (req, res) => {
res.status(200).json({ status: "OK" });
});
// The "catchall" handler: for any request that doesn't
// match one above, send back React's index.html file.
app.get("*", (req, res) => {
res.sendFile(path.join(__dirname + "/client/build/index.html"));
});
app.use(middleware.notFound);
app.use(middleware.handleError);
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Server running on port ${port}`));
module.exports = app;