-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
44 lines (35 loc) · 980 Bytes
/
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
const express = require("express")
const app = express()
const apiRouter = require('./routes/api-router');
const cors = require('cors');
app.use(cors());
app.use(express.json());
app.use('/api', apiRouter);
//ERROR HANDLING
app.all("*", (req, res) => {
res.status(400).send({ msg: "Invalid path" });
});
app.use((err, req, res, next) => {
if (err.status && err.msg) {
res.status(err.status).send({ msg: err.msg });
} else next(err);
});
app.use((err, req, res, next) => {
if (err.code === "22P02") {
res.status(400).send({ msg: "Bad request" });
} else next(err);
});
app.use((err, req, res, next) => {
if (err.code === "23502") {
res.status(400).send({ msg: "Bad request" });
} else next(err);
});
app.use((err, req, res, next) => {
if (err.code === "23503") {
res.status(404).send({ msg: "Not found" });
} else next(err);
});
app.use((err, req, res, next) => {
res.status(500).send("Internal server error");
});
module.exports = app;