-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
executable file
·45 lines (34 loc) · 1011 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
38
39
40
41
42
43
44
45
const express = require("express");
const mongoose = require("mongoose");
const labelRouter = require("./routes/v1/label");
// Import dotenv
require("dotenv").config();
const app = express();
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(express.static("client/build"));
mongoose.connect(
process.env.mongoURL,
{ useUnifiedTopology: true, useNewUrlParser: true },
err => {
console.log(err ? err : "Connected to DB");
}
);
app.use("/api/v1/labels", labelRouter);
if (process.env.NODE_ENV === "production") {
app.get("*", (req, res) => {
res.sendFile(path.resolve(__dirname, "client", "build", "index.html"));
});
}
// catch 404 and forward to error handler
app.use(function(req, res) {
res.status(404).json({ message: "Page Not Found" });
});
// error handler
app.use(function(err, req, res, next) {
res.json({ err });
});
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});