-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (45 loc) · 1.18 KB
/
index.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
const express = require("express");
const cookieParser = require("cookie-parser");
const bodyParser = require("body-parser");
const csrf = require("csurf");
const helmet = require("helmet");
const csrfProtection = csrf({ cookie: true });
const app = express();
app.use(helmet());
// http
// cliente <-> servidor
// Verbo + URL
// Verbos http: GET, PATCH, POST, DELETE
// Url: http://localhost:3000/hello-world GET
// API
const db = {}
// camel case
function handleGetHelloWorld(req, res) {
console.log(req.propiedadArbitraria);
return res.json(db);
}
function handlePostHelloWorld(req, res) {
const id = Math.random();
db[id] = req.body;
return res.send("Hola desde POST");
}
function testMiddleware1(req, res, next) {
console.log("HOLA DESDE EL MIDDLEWARE");
req.propiedadArbitraria = 100;
next();
}
function testMiddleware2(req, res, next) {
console.log("HOLA ESTOY EN EL 2");
next();
}
app.use(bodyParser.json());
app.use(cookieParser());
app.use(testMiddleware1);
app.use(testMiddleware2);
app
.route("/hello-world")
.get(csrfProtection, handleGetHelloWorld)
.post(handlePostHelloWorld);
app.listen(3000, () => {
console.log("Servidor corriendo en puerto 3000...");
});