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: Routing.md
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -111,12 +111,12 @@ app.all("/", function (req, res) {
111
111
112
112
Routejs supports route parameters, route parameters are used for dynamic routing.
113
113
114
-
In routejs we can define route parameters using `{}` syntax, we can put any name inside curly brackets and we can access them using `req.params` object.
114
+
In routejs we can define route parameters using `:` syntax, we can put any name inside curly brackets and we can access them using `req.params` object.
115
115
116
116
Example:
117
117
118
118
```js
119
-
app.get("/user/{name}", function (req, res) {
119
+
app.get("/user/:name", function (req, res) {
120
120
// Get route parameters using params object
121
121
res.end(req.params.name);
122
122
});
@@ -141,7 +141,7 @@ Make sure to escape all regular expression special characters, otherwise you mig
141
141
142
142
Named regular expression:
143
143
```js
144
-
app.get("/user/{id:(\\d+)}", function (req, res) {
144
+
app.get("/user/:id:(\\d+)", function (req, res) {
145
145
res.end(req.params.id);
146
146
});
147
147
```
@@ -235,7 +235,7 @@ If you are using host based url routing, please make sure to set the default hos
235
235
Set default host to all routes and middlewares:
236
236
```js
237
237
constapp=newRouter({
238
-
host:"localhost:3000"
238
+
host:"localhost"
239
239
});
240
240
```
241
241
@@ -244,13 +244,13 @@ If we set default request host to all routes and middlewares, then they are only
244
244
Example:
245
245
246
246
```js
247
-
// Route is only executed when request host is localhost:3000
247
+
// Route is only executed when request host is localhost
248
248
app.get("/", function (req, res) {
249
249
res.end("Ok");
250
250
});
251
251
252
-
// Route is only executed when request host is blog.localhost:3000
253
-
app.domain("blog.localhost:3000", function (router) {
252
+
// Route is only executed when request host is blog.localhost
253
+
app.domain("blog.localhost", function (router) {
254
254
router.get("/", function (req, res) {
255
255
res.end("Blog");
256
256
});
@@ -262,7 +262,7 @@ Host parameters:
262
262
Routejs supports named parameters and regular expression in host based url routing.
263
263
264
264
```js
265
-
app.domain("{name}.localhost:3000", function (router) {
0 commit comments