Skip to content

Commit 06a48ca

Browse files
Update README.md
1 parent 06e6332 commit 06a48ca

1 file changed

Lines changed: 62 additions & 0 deletions

File tree

README.md

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -467,3 +467,65 @@ Calling the global
467467
```ruby
468468
app.use(myLogger)
469469
```
470+
471+
>Let see the simple function for req and response with middleware call by particular method
472+
473+
```ruby
474+
var express = require('express')
475+
var app = express()
476+
477+
478+
// Logging of original URL
479+
function logOriginalUrl (req, res, next) {
480+
console.log('Request URL:', req.originalUrl)
481+
next()
482+
}
483+
484+
485+
// Log methods
486+
function logMethod (req, res, next) {
487+
console.log('Request Type:', req.method)
488+
next()
489+
}
490+
491+
var logStuff = [logOriginalUrl, logMethod]
492+
app.get('/user/:id', logStuff, function (req, res, next) {
493+
res.send('User Info')
494+
})
495+
496+
497+
app.listen(3000, () => console.log("Application running on 3000 port"))
498+
499+
500+
```
501+
502+
here we have two middleware component one for get the original URL for the requesest and another one is to get the type of method which hit there.
503+
504+
```ruby
505+
// Logging of original URL
506+
function logOriginalUrl (req, res, next) {
507+
console.log('Request URL:', req.originalUrl)
508+
next()
509+
}
510+
511+
512+
// Log methods
513+
function logMethod (req, res, next) {
514+
console.log('Request Type:', req.method)
515+
next()
516+
}
517+
```
518+
519+
Now creating the array of two middleware here as like below.
520+
521+
```ruby
522+
var logStuff = [logOriginalUrl, logMethod]
523+
```
524+
525+
Plug that method which we need to attach that middle ware
526+
```ruby
527+
app.get('/user/:id', *logStuff*, function (req, res, next) {
528+
res.send('User Info')
529+
})
530+
531+
```

0 commit comments

Comments
 (0)