-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (50 loc) · 1.7 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// index.js
const express = require("express");
const app = express();
const morgan = require("morgan");
var fs = require("fs");
const helmet = require("helmet");
var path = require("path");
const cors = require("cors");
const ItemRouter = require("./routes/itemRoute.route");
const connection = require("./connection");
const loggingMiddleware = require("./middlewares/loggingMiddleware.middleware");
const errorMiddleware = require("./middlewares/errorMiddleware.middleware");
const DeliveryVehicleRouter = require("./routes/deliveryVehicleRoutes.route");
const userRouter = require("./routes/userRoute.route");
const OrderRouter = require("./routes/orderRoutes.route");
const CustomerRouter = require("./routes/customerRoutes.route");
const authenticateToken = require("./middlewares/authMiddleware.middleware");
require("dotenv").config({ debug: true });
const PORT = process.env.PORT || 3001;
app.use(cors());
app.use(express.json());
// helmet for security purpose
app.use(helmet());
app.use(
morgan(loggingMiddleware, {
stream: fs.createWriteStream(path.join(__dirname, "access.log"), {
flags: "a",
}),
})
);
app.use("/api", userRouter);
// Use the routes
app.use("/api", ItemRouter);
app.use(authenticateToken);
// Use '/api' as the base URL for your routes
app.use("/api", DeliveryVehicleRouter);
app.use("/api", OrderRouter);
app.use("/api", CustomerRouter);
// Use the error middleware after all routes
app.use(errorMiddleware);
app.listen(PORT, async () => {
try {
await connection;
console.log("Backend connected to MongoDB");
} catch (err) {
console.log(err.message);
console.log("Backend not connected to MongoDB");
}
console.log(`Server running at port : ${PORT} `);
});