This repository has been archived by the owner on May 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
65 lines (57 loc) · 1.47 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
61
62
63
64
65
const express = require("express");
const morgan = require("morgan");
const bodyParser = require("body-parser");
const cors = require("cors");
const cookieParser = require("cookie-parser");
const app = express();
// DOTENV CONFIG
const dotenv = require("dotenv");
dotenv.config({ path: "./config.env" });
// MONGO CONFIG
const mongoConnect = require("./src/config/mongo");
mongoConnect();
// BODY AND COOKIE PARSER
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cookieParser());
app.use(morgan("dev"));
// CORS
const allowedOrigins = ['localhost', 'postman-origin'];
const corsOptions = {
origin: function (origin, callback) {
if (allowedOrigins.includes(origin)) {
callback(null, true);
} else {
callback(null, true);
}
},
credentials: true,
methods: ["GET", "POST", "PUT", "DELETE"],
};
app.use(cors(corsOptions));
// ROUTES
app.get("/", (req, res) => {
res.send("BACKEND TIKET.IN");
});
app.use("/user", require("./src/routes/user"));
app.use("/admin", require("./src/routes/admin"));
// ERROR HANDLING
app.use((req, res, next) => {
const error = new Error("Not found!");
error.status = 404;
next(error);
});
app.use((error, req, res, next) => {
res.status(error.status || 500);
res.json({
error: {
message: error.message,
},
});
});
// SERVER START
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Server is running on port ${port}.`);
});
module.exports = app;