-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.js
43 lines (37 loc) · 1.21 KB
/
driver.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
const express = require("express");
const app = express();
const hateoasLinks = require("express-hateoas-links");
const articleRoutes = require("./routes/article");
const commentRoutes = require("./routes/comment");
const tagRoutes = require("./routes/tag");
const userRoutes = require("./routes/user");
const loginRoutes = require("./routes/login");
const homeRoutes = require("./routes/home");
const bodyParser = require("body-parser");
//Usando middleware para extraer el cuerpo de la petición
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
//Sobreescribe la función res.json() con una función nueva que acepta un arreglo de links
app.use(hateoasLinks);
/**
* Cargando rutas
*/
app.use("/", homeRoutes);
app.use("/login", loginRoutes);
app.use("/users", userRoutes);
app.use("/articles", articleRoutes);
app.use("/comments", commentRoutes);
app.use("/tags", tagRoutes);
/**
* Si el recurso no existe, devuelve 404
*/
app.get("*", (req, res) => {
res.status(404);
res.header("Content-Type", "application/problem+json");
res.json({
title: "Resource not found",
detail: "The requested resource could not be found",
status: 404
});
});
module.exports = app;