-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
64 lines (57 loc) · 1.55 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
62
63
64
const express = require("express");
const path = require("path");
const cookieParser = require("cookie-parser");
const logger = require("morgan");
const cors = require("cors");
const indexRouter = require("./routes/index");
const mongoose = require("mongoose");
const { AppError, sendResponse } = require("./helpers/utils");
require("dotenv/config");
// const mongoURI = process.env.MONGO_URI;
const atlasMongoURI = process.env.ATLASMONGO_URI;
const app = express();
app.use(cors());
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")));
// Connect to MONGODB
// mongoose.connect(process.env.MONGO_URI, () => {
// console.log('Connected to Database!');
// });
// mongoose
// .connect(mongoURI)
// .then(() => {
// console.log(`DB connected ${mongoURI}`);
// })
// .catch((err) => {
// console.log(err);
// });
mongoose
.connect(atlasMongoURI)
.then(() => {
console.log(`DB connected ${atlasMongoURI}`);
})
.catch((err) => {
console.log(err);
});
app.use("/", indexRouter);
//check wrong router
app.use((req, res, next) => {
const err = new AppError(404, "Not Found", "Bad Request");
next(err);
});
/* Initialize Error Handling */
app.use((err, req, res, next) => {
console.log("ERROR", err);
return sendResponse(
res,
err.statusCode ? err.statusCode : 500,
false,
null,
{ message: err.message },
err.isOperational ? err.errorType : "Internal Server Error"
);
});
module.exports = app;