-
Notifications
You must be signed in to change notification settings - Fork 19
/
app.js
51 lines (42 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
const express = require("express");
const path = require("path");
const cookieParser = require("cookie-parser");
const logger = require("morgan");
const swaggerUi = require("swagger-ui-express");
require("dotenv").config();
const indexRouter = require("./routes/index");
const smsRouter = require("./routes/sms");
const apiResponse = require("./helpers/apiResponse");
const swaggerDocument = require("./docs/swagger.json");
const mongoDbConnection =require("./configs/dbconfig");
const cors = require("cors");
const auth = require("./middleware/auth");
const jwt = require('jsonwebtoken');
//load the database
mongoDbConnection();
const app = express();
//don't show the log when it is test
if (process.env.NODE_ENV !== "test") {
app.use(logger("dev"));
}
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, "public")));
// init swagger doc
app.use("/api/v1/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocument));
//To allow cross-origin requests
app.use(cors());
//Route Prefixes
app.use("/web", indexRouter);
app.use("/v1", auth, smsRouter);
// 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);
}
});
module.exports = app;