-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path06_MiddleWareinExpress.js
60 lines (48 loc) · 2.16 KB
/
06_MiddleWareinExpress.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
59
60
// Middlewares in Express
// Middleware functions are functions that have access to the request
// object (req), the response object (res), and the next middleware
// function in the application’s request-response cycle. The next
// middleware function is commonly denoted by a variable named next.
// Middleware functions can perform the following tasks:
// Execute any code.
// Make changes to the request and the response objects.
// End the request-response cycle.
// Call the next middleware function in the stack.
// If the current middleware function does not end the request-response
// cycle, it must call next() to pass control to the next middleware
// function. Otherwise, the request will be left hanging.
// Application-level middleware
// Bind application-level middleware to an instance of the app
// object by using the app.use() and app.METHOD() functions, where
// METHOD is the HTTP method of the request that the middleware
// function handles (such as GET, PUT, or POST) in lowercase.
// This example shows a middleware function with no mount path.
// The function is executed every time the app receives a request.
const express = require('express');
const app = express();
app.use(function (req, res, next) {
console.log('Time:', Date.now())
next()
})
// This example shows a middleware function mounted on the
// /user/:id path. The function is executed for any type of HTTP
// request on the /user/:id path.
app.use('/user/:id', function (req, res, next) {
console.log('Request Type:', req.method)
next()
})
// Router-level middleware
// Router-level middleware works in the same way as application-level
// middleware, except it is bound to an instance of express.Router().
const router = express.Router()
// Load router-level middleware by using the router.use() and
// router.METHOD() functions.
// The following example code replicates the middleware system
// that is shown above for application-level middleware, by using
// router-level middleware:
// a middleware function with no mount path. This code is
// executed for every request to the router
router.use(function (req, res, next) {
console.log('Time:', Date.now())
next()
})