-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
50 lines (42 loc) · 1.45 KB
/
server.js
File metadata and controls
50 lines (42 loc) · 1.45 KB
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
const express = require("express");
const dotenv = require("dotenv");
const userRouter = require("./routes/users/userRoutes");
const postRouter = require("./routes/posts/postRoutes");
const categoryRouter = require("./routes/categories/categoryRoute");
const commentRouter = require("./routes/comments/commentRouter");
const globalErrorHandler = require("./middlewares/globalErrorHandler");
const Post = require("./model/Post/Post");
dotenv.config();
require("./config/dbConnect");
const app = express();
// MIDDLEWARE
app.use(express.json()); // parse incoming payload
// app.use(isAdmin); // if implemented like this login and register should also have authorization header of currently logged-in user which does not make sense
// ROUTES
// --Home route--
app.get("/",async(req,res)=>{
try {
const posts = await Post.find();
res.json({
status: "success",
data : posts
})
} catch (error) {
res.json(error)
}
});
app.use("/api/v1/users",userRouter);
app.use("/api/v1/posts", postRouter);
app.use("/api/v1/comments", commentRouter);
app.use("/api/v1/categories", categoryRouter);
// ERROR HANDLING MIDDLEWARE
app.use(globalErrorHandler);
// 404 error
app.use("*", (req,res)=>{
res.status(404).json({
message : `${req.originalUrl} - Route Not Found`
});
});
// LISTEN TO SERVER
const PORT = process.env.PORT || 9000
app.listen(PORT, console.log(`Server is up and running on ${PORT}`));