-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
46 lines (37 loc) · 1.2 KB
/
index.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
const express = require("express");
const app = express();
//
const apiError = require("./utils/apiError");
const { globalErrHandler } = require("./utils/globalErrHandler");
// access environment variables
require("dotenv").config();
// connect to database
require("./config/database");
// middleware
app.use(express.json()); // pass income payload
// routes
const userRouters = require("./routes/User");
const authRouters = require("./routes/Auth");
const categoryRouters = require("./routes/Category");
const postRouters = require("./routes/Post");
const commentRouters = require("./routes/Comment");
// routes middlware
app.use("/api/users", userRouters);
app.use("/api/auth", authRouters);
app.use("/api/categories", categoryRouters);
app.use("/api/posts", postRouters);
app.use("/api/comments", commentRouters);
// 404 error
app.all("*", (req, res, next) => {
// create error
const err = new apiError(`Can't find this route ${req.originalUrl}`, 400);
// send it to Global errors handling middlware
next(err);
});
// Global Error Handlers Middleware
app.use(globalErrHandler);
// Listen To Server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`server is running on ${PORT}`);
});