Skip to content

Commit 63cb61d

Browse files
committed
middleware
1 parent ed7412c commit 63cb61d

6 files changed

Lines changed: 561 additions & 0 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ day10-expressTest/node_modules/
4444
day11-ExpressAppGenerator/node_modules/
4545
day12-StaticFiles/node_modules/
4646
day13-BasicRouting/node_modules/
47+
day14-Middleware/node_modules/
4748

4849
# TypeScript v1 declaration files
4950
typings/

day13-BasicRouting/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ app.get('/users/:userId', function (req, res) {
5858
res.send(req.params)
5959
})
6060

61+
// If you want passing value whould not mandatory than you can use
62+
app.get('/users/:userId?', function (req, res) {
63+
res.send(req.params)
64+
})
65+
6166
// You can pass the mutiple paramnter on route URL as like below.
6267
// Input http://localhost:4000/users/50/books/5
6368
// Output {"userId":"50","bookId":"5"}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// How to call the middle ware only for one function not for all
2+
3+
4+
// Let see the simple function for req and response
5+
6+
var express = require('express')
7+
var app = express()
8+
9+
10+
// Logging of original URL
11+
function logOriginalUrl (req, res, next) {
12+
console.log('Request URL:', req.originalUrl)
13+
next()
14+
}
15+
16+
17+
// Log methods
18+
function logMethod (req, res, next) {
19+
console.log('Request Type:', req.method)
20+
next()
21+
}
22+
23+
var logStuff = [logOriginalUrl, logMethod]
24+
app.get('/user/:id', logStuff, function (req, res, next) {
25+
res.send('User Info')
26+
})
27+
28+
29+
app.listen(3000, () => console.log("Application running on 3000 port"))

day14-Middleware/index.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Middleware is using between the req recive from client and before responding to it.
2+
// In Middleware we have three paramenter req, res and the new one is next.
3+
// You can see more detail from expressJs website
4+
// https://expressjs.com/en/guide/writing-middleware.html
5+
//Middleware functions can perform the following tasks:
6+
7+
// > Execute any code.
8+
// > Make changes to the request and the response objects.
9+
// > End the request-response cycle.
10+
// > Call the next middleware in the stack.
11+
12+
13+
// Let see the simple function for req and response
14+
15+
var express = require('express')
16+
var app = express()
17+
18+
19+
// Small middle ware for logging, but you need to include it to application.
20+
// he next() function is not a part of the Node.js or Express API, but is the third argument that is passed to the middleware function.
21+
22+
var myLogger = function (req, res, next) {
23+
console.log('LOGGED')
24+
next()
25+
}
26+
27+
// To load the middleware function, call app.use(), specifying the middleware function.
28+
// Calling the global
29+
app.use(myLogger)
30+
31+
32+
app.get('/', function (req, res) {
33+
res.send('Hello World!')
34+
})
35+
36+
37+
38+
app.listen(3000, () => console.log("Application running on 3000 port"))

0 commit comments

Comments
 (0)