-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.js
More file actions
37 lines (30 loc) · 809 Bytes
/
server.js
File metadata and controls
37 lines (30 loc) · 809 Bytes
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
const express = require("express");
require("dotenv").config();
const booksRouter = require("./booksRouter");
const adminRouter = require("./adminRouter");
const app = express();
app.use(express.json());
const PORT = process.env.PORT || 8000;
app.use("/books", booksRouter);
app.use("/admin", adminRouter);
// 404 handler
app.use((req, res, next) => {
res.status(404).json({
message: "The requested resource was not found!",
});
});
// error handling middleware
app.use((err, req, res, next) => {
if (err.message) {
res.status(500).json({
mesage: err.message,
});
} else {
res.status(500).json({
mesage: "An unknown error occured!",
});
}
});
app.listen(PORT, () => {
console.log(`Listening on port ${PORT}`);
});