This repository has been archived by the owner on Oct 29, 2024. It is now read-only.
forked from tranlehai266/online-shop-BE
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
62 lines (58 loc) · 1.5 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
require("dotenv").config();
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 { sendResponse } = require("./helpers/utils");
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")));
app.use("/images", express.static(path.join(__dirname, "./images")));
app.use(
"/product-images",
express.static(path.join(__dirname, "./imagesProducts"))
);
app.use("/api", indexRouter);
const mongoose = require("mongoose");
const mongoURI = process.env.MONGODB_URI;
mongoose
.connect(mongoURI)
.then(() => console.log("DB connected "))
.catch((err) => console.log(err));
// Error Handle
// Catch 404
// asdasdasdsa
app.use((req, res, next) => {
const err = new Error("Not Found");
err.statusCode = 404;
next(err);
});
app.use((err, req, res, next) => {
console.log("ERROR", err);
if (err.isOperational) {
return sendResponse(
res,
err.statusCode ? err.statusCode : 500,
false,
null,
{ message: err.message },
err.errorType
);
} else {
return sendResponse(
res,
err.statusCode ? err.statusCode : 500,
false,
null,
{ message: err.message },
"Internal Server Error"
);
}
});
module.exports = app;