-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
70 lines (60 loc) · 1.61 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
66
67
68
69
70
const express = require("express");
const mongo = require("mongoose");
const user = require("./Routes/routes");
const verify = require("./Controller/Misc/auth-verify");
const errorHandler = require("./Controller/Misc/errorHandler");
if (process.env.NODE_ENV != "production") {
require("dotenv").config();
}
const main = (app, mongodbURI) => {
app.disable("etag");
mongo.connect(
mongodbURI,
{ useNewUrlParser: true, useUnifiedTopology: true },
(err) => {
if (err) {
console.log(err);
}
console.log("Connected to DB");
}
);
app.use(express.json());
app.use(function (req, res, next) {
res.header("X-Powered-by", "SecAuth-Express");
next();
});
app.use("/api/", user);
//404 error Handler
app.use((req, res, next) => {
return next(new errorHandler.AppError("You are on wrong way!!", 404));
});
//Error Handler
const sendErrorDev = (err, res) => {
res.status(err.statusCode).json({
status: err.status,
error: err,
message: err.message,
stack: err.stack,
});
};
const sendErrorProd = (err, res) => {
res.status(err.statusCode).json({
success: false,
status: err.status,
message: err.message,
});
};
app.use((err, req, res, next) => {
err.statusCode = err.statusCode || 500;
err.status = err.status || "error";
mode = process.env.NODE_ENV || "production";
if (mode === "development") {
sendErrorDev(err, res);
} else {
sendErrorProd(err, res);
}
});
};
module.exports.init = main;
module.exports.verifyUser = verify;
module.exports.errorHandler = errorHandler;