-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
61 lines (50 loc) · 1.44 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
60
61
const express = require("express");
const morgan = require("morgan");
const mongoose = require("mongoose");
var apiRouter = require("./routes/api");
var apiResponse = require("./src/user/helpers/apiResponse");
require("dotenv").config();
const bodyParser = require("body-parser");
const restrictOrigin = require("./middlewares/restrictOrigin");
var cors = require("cors");
const app = express();
mongoose
.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("Database connection Success!");
})
.catch((err) => {
console.error("Mongo Connection Error", err);
});
const PORT = process.env.PORT || 5000;
app.use(restrictOrigin);
app.use(express.json());
app.use(morgan("dev"));
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())
//To allow cross-origin requests
app.use(cors());
//Route Prefixes
app.use("/api/", apiRouter);
// throw 404 if URL not found
app.all("*", function(req, res) {
return apiResponse.notFoundResponse(res, "Page not found");
});
app.use((err, req, res) => {
if(err.name == "UnauthorizedError"){
return apiResponse.unauthorizedResponse(res, err.message);
}
});
app.get("/ping", (req, res) => {
return res.send({
status: "Server is up and running",
});
});
app.listen(PORT, () => {
console.log("Server started listening on port : ", PORT);
});