You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+120-1Lines changed: 120 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2584,7 +2584,126 @@ app.post('/api/users', jsonParser, function (req, res) {
2584
2584
<b><a href="#">↥ back to top</a></b>
2585
2585
</div>
2586
2586
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.
0 commit comments