-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
45 lines (36 loc) · 979 Bytes
/
server.js
File metadata and controls
45 lines (36 loc) · 979 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
38
39
40
41
42
43
44
45
import express, { application } from "express";
import dotenv from "dotenv";
import cors from "cors";
import dbConnection from "./database/connection.js";
import productRouter from "./routes/productRoutes.js";
import userRouter from "./routes/userRoutes.js";
dotenv.config();
const app = express();
// mongodb database connection
dbConnection();
// Built-in middlewares
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Third-party middlewares
app.use(cors());
// Error handling middlewares
app.use((err, req, res, next) => {
console.error(err.stack);
res.status(500).send({
status: 500,
message: err.message,
body: {},
});
});
// application-level middleware
app.use("/api/v1/product", productRouter);
app.use("/api/v1/", userRouter);
// Route
app.get("/", (req, res) => {
res.send("Hi");
});
// create server
const PORT = process.env.PORT || 3002;
app.listen(PORT, () => {
console.log(`Server running on ${PORT}`);
});