-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
31 lines (25 loc) · 816 Bytes
/
server.js
File metadata and controls
31 lines (25 loc) · 816 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
const express = require("express");
const app = express();
const PORT = 3000;
app.use(express.json());
// Import routes
const userRoutes = require("./routes/users.routes");
const postRoutes = require("./routes/posts.routes");
const commentRoutes = require("./routes/comments.routes");
// Use routes
app.use("/users", userRoutes);
app.use("/posts", postRoutes);
app.use("/comments", commentRoutes);
// Error handling middleware
app.use((err, req, res, next) => {
console.error(err.stack);
if (err.isOperational) {
// Known, operational error
return res.status(err.statusCode).json({ error: err.message });
}
// Unknown or programming errors
res.status(500).json({ error: "Internal Server Error" });
});
app.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});