-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
62 lines (53 loc) · 1.33 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
const express = require("express");
const mongoose = require("mongoose");
const createError = require("http-errors");
const morgan = require("morgan");
// const path = require("path");
// const fs = require("fs");
const { PORT } = require("./config/config");
const app = express();
// Connect to MongoDB
mongoose
.connect(process.env.MONGODB_URI)
.then(() => {
console.log("Connected to DB.");
})
.catch((err) => {
console.log("DB error : ", err);
});
// logging
if (process.env.NODE_ENV === "production") {
// const accessLogStream = fs.createWriteStream(
// path.join(__dirname, "access.log"),
// {
// flags: "a",
// }
// );
// app.use(morgan("combined", { stream: accessLogStream }));
app.use(morgan("combined"));
} else {
app.use(morgan("dev"));
}
// body parser
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.get("/", (req, res, next) => {
res.send("Server is up!");
});
// all api routes
app.use("/api", require("./routes"));
// catch 404
app.use((req, res, next) => {
next(createError.NotFound());
});
// catch errors
app.use((err, req, res, next) => {
res.status(err.status || 500);
res.send({
status: err.status || 500,
message: err.message,
});
});
app.listen(PORT, () =>
console.log(`Server started on http://localhost:${PORT}`)
);