-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample-express-es6.js
More file actions
38 lines (28 loc) · 967 Bytes
/
example-express-es6.js
File metadata and controls
38 lines (28 loc) · 967 Bytes
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
const express = require('express');
const app = express();
// App level middlewares
// app.use
// app.METHOD get post put
// Router / Route level middlewares
// 3rd party middlewares ( npm...)
// Error handling middlewares
const setDaniMiddleware = () => (req, res, next) => {
req.dani = 'dani';
console.log('Setting dani to the request object');
next();
}
const printDaniMiddleware = () => (req, res, next) => {
console.log('Printing dani: ', req.dani);
next();
}
const sendDaniMiddleware = () => (req, res, next) => {
console.log('res.send(), res.json() and res.end() end the middleware chain');
res.status(200).json({ dani: 'I am JASON Statham' });
}
// ( function that returns a function! ;) )
const doStuffBeforeMiddleware = () => (req, res, next) => {
console.log('This middleware does stuff at app level');
next();
}
app.use(doStuffBeforeMiddleware());
app.get('/', setDaniMiddleware(), printDaniMiddleware(), sendDaniMiddleware());