-
Notifications
You must be signed in to change notification settings - Fork 12
/
app.js
59 lines (50 loc) · 1.41 KB
/
app.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
const path = require("path");
const express = require("express");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const helmet = require("helmet"); // secure headers
const compression = require("compression"); // compress assets
const morgan = require("morgan"); // logging
const { MONGODB_URI } = require("./config/AppConst");
/**
* Controllers
*/
const AppError = require("./controllers/errorController");
/**
* Routes
*/
const userRoutes = require("./routes/userRoutes");
const foodRoutes = require("./routes/foodsRoute");
const adminRoutes = require("./routes/adminRoutes");
const app = express();
/**
* Database Connection
*/
mongoose
.connect(MONGODB_URI, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true,
})
.then(() => {})
.catch((err) => {
console.log(err);
});
/**
* Middlewares
*/
app.use(helmet());
app.use(compression());
app.use(bodyParser.json());
app.use("/images", express.static(path.join(__dirname, "images")));
app.use("/user", userRoutes); // --- User Acccess
app.use("/food", foodRoutes); // -- Product Access
app.use("/admin", adminRoutes); // --- Admin Access
app.use(AppError.unAuthorised); // -- Error Handler
app.use((error, req, res, next) => {
const status = error.statusCode || 500;
const data = error.data;
res.status(status).json({ data: data });
});
const PORT = process.env.PORT || 8000;
app.listen(PORT);