Skip to content

Commit fe8b38e

Browse files
committed
Node.js Routing
1 parent 5292e80 commit fe8b38e

File tree

1 file changed

+120
-1
lines changed

1 file changed

+120
-1
lines changed

README.md

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2584,7 +2584,126 @@ app.post('/api/users', jsonParser, function (req, res) {
25842584
<b><a href="#">↥ back to top</a></b>
25852585
</div>
25862586

2587-
#### Q. ***How does routing work in Node.js?***
2587+
## Q. ***How does routing work in Node.js?***
2588+
2589+
Routing defines the way in which the client requests are handled by the application endpoints. We define routing using methods of the Express app object that correspond to HTTP methods; for example, `app.get()` to handle `GET` requests and `app.post` to handle `POST` requests, `app.all()` to handle all HTTP methods and `app.use()` to specify middleware as the callback function.
2590+
2591+
These routing methods "listens" for requests that match the specified route(s) and method(s), and when it detects a match, it calls the specified callback function.
2592+
2593+
*Syntax*:
2594+
2595+
```js
2596+
app.METHOD(PATH, HANDLER)
2597+
```
2598+
2599+
Where:
2600+
2601+
* app is an instance of express.
2602+
* METHOD is an `HTTP request method`.
2603+
* PATH is a path on the server.
2604+
* HANDLER is the function executed when the route is matched.
2605+
2606+
**a) Route methods**
2607+
2608+
```js
2609+
// GET method route
2610+
app.get('/', function (req, res) {
2611+
res.send('GET request')
2612+
})
2613+
2614+
// POST method route
2615+
app.post('/login', function (req, res) {
2616+
res.send('POST request')
2617+
})
2618+
2619+
// ALL method route
2620+
app.all('/secret', function (req, res, next) {
2621+
console.log('Accessing the secret section ...')
2622+
next() // pass control to the next handler
2623+
})
2624+
```
2625+
2626+
**b) Route paths**
2627+
2628+
Route paths, in combination with a request method, define the endpoints at which requests can be made. Route paths can be strings, string patterns, or regular expressions.
2629+
2630+
The characters `?`, `+`, `*`, and `()` are subsets of their regular expression counterparts. The hyphen `(-)` and the dot `(.)` are interpreted literally by string-based paths.
2631+
2632+
*Example*:
2633+
2634+
```js
2635+
// This route path will match requests to /about.
2636+
app.get('/about', function (req, res) {
2637+
res.send('about')
2638+
})
2639+
2640+
2641+
// This route path will match acd and abcd.
2642+
app.get('/ab?cd', function (req, res) {
2643+
res.send('ab?cd')
2644+
})
2645+
2646+
2647+
app.get(/.*fly$/, function (req, res) {
2648+
res.send('/.*fly$/')
2649+
})
2650+
```
2651+
2652+
**c) Route parameters**
2653+
2654+
Route parameters are named URL segments that are used to capture the values specified at their position in the URL. The captured values are populated in the `req.params` object, with the name of the route parameter specified in the path as their respective keys.
2655+
2656+
*Example*:
2657+
2658+
```js
2659+
app.get('/users/:userId', function (req, res) {
2660+
res.send(req.params)
2661+
})
2662+
```
2663+
2664+
**Response methods**
2665+
2666+
| Method | Description |
2667+
|-------------------|-------------------------------|
2668+
|`res.download()` |Prompt a file to be downloaded.|
2669+
|`res.end()` |End the response process.|
2670+
|`res.json()` |Send a JSON response.|
2671+
|`res.jsonp()` |Send a JSON response with JSONP support.|
2672+
|`res.redirect()` |Redirect a request.|
2673+
|`res.render()` |Render a view template.|
2674+
|`res.send()` |Send a response of various types.|
2675+
|`res.sendFile()` |Send a file as an octet stream.|
2676+
|`res.sendStatus()` |Set the response status code and send its string representation as the response body.|
2677+
2678+
**d) Router method**
2679+
2680+
```js
2681+
var express = require('express')
2682+
var router = express.Router()
2683+
2684+
// middleware that is specific to this router
2685+
router.use(function timeLog (req, res, next) {
2686+
console.log('Time: ', Date.now())
2687+
next()
2688+
})
2689+
2690+
// define the home page route
2691+
router.get('/', function (req, res) {
2692+
res.send('Birds home page')
2693+
})
2694+
2695+
// define the about route
2696+
router.get('/about', function (req, res) {
2697+
res.send('About birds')
2698+
})
2699+
2700+
module.exports = router
2701+
```
2702+
2703+
<div align="right">
2704+
<b><a href="#">↥ back to top</a></b>
2705+
</div>
2706+
25882707
#### Q. ***How Node prevents blocking code?***
25892708
#### Q. ***What is difference between promise and async await in Node.js?***
25902709
#### Q. ***How to use JSON Web Token (JWT) for authentication in Node.js?***

0 commit comments

Comments
 (0)