-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
58 lines (44 loc) · 1.47 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
const express = require("express");
const morgan = require("morgan");
const AppError = require("./utils/appError");
const globalErrorHandler = require("./controllers/errorController");
const tourRouter = require("./routes/tourRoutes");
const userRouter = require("./routes/userRoutes");
const app = express();
// MIDDLEWARES
console.log(process.env.NODE_ENV);
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
}
app.use(express.json()); // permite guardar dados no req da requisição POST
app.use((req, res, next) => {
req.requestTime = new Date().toISOString();
console.log(req.headers);
next();
});
// User Source
// ROUTES
// app.get("/api/v1/tours", getAllTours);
// app.get("/api/v1/tours/:id", getTour);
// app.post("/api/v1/tours", createTour);
// app.patch("/api/v1/tours/:id");
// app.delete("/api/v1/tours/:id");
// app.use("/api/v1/", tourRouter);
app.use("/api/v1/tours", tourRouter);
app.use("/api/v1/users", userRouter);
app.all("*", (req, res, next) => {
// 1ª tratamento de erro
// res.status(404).json({
// status: "fail",
// message: `Cannot find (${req.originalUrl})on this Server!!!`,
// });
// 2º mockando erro
// const err = new Error(`Cannot find (${req.originalUrl})on this Server!!!`);
// err.status = "fail";
// err.statusCode = 404;
// 3 tratamento de erro
next(new AppError(`Cannot find (${req.originalUrl})on this Server!!!`, 404));
});
app.use(globalErrorHandler);
// START SERVER
module.exports = app;