-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
43 lines (34 loc) · 1.18 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
// create the server
const express = require("express");
const app = express();
// enable CORS
app.use(require("cors")());
// parse JSON in request body
app.use(express.json());
// apply JWT middleware
app.use(require("./middleware/auth").authenticateJWT);
// routes for resources
app.use("/auth", require("./routes/auth"));
app.use("/users", require("./routes/users"));
app.use("/products", require("./routes/products"));
app.use("/carts", require("./routes/carts"));
app.use("/cartItems", require("./routes/cartItems"));
app.use("/orders", require("./routes/orders"));
app.use("/customers", require("./routes/customers"));
app.use("/payments", require("./routes/payments"));
// generate custom errors
const ExpressError = require("./ExpressError");
// handle 404s AKA unmatched requests
app.use((req, res, next) =>
next(new ExpressError("Not found.", 404))
);
// generic error handler catch all
app.use((err, req, res, next) => {
const statusCode = err.statusCode || 500;
const message = err.message || "Something went wrong.";
if (process.env.NODE_ENV !== "test") {
console.error(err.stack);
}
return res.status(statusCode).json({ error: { statusCode, message } });
});
module.exports = app;