Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Update README.md

  • Loading branch information...
commit 202ca93a9bfdab6b76737e32fac2ec85167b308e 1 parent 43300c5
@tarruda authored
Showing with 7 additions and 1 deletion.
  1. +7 −1 README.md
View
8 README.md
@@ -92,11 +92,13 @@ router.get('/queryable/<options:query>', function(req, res) {
Can be used to write middlewares, just like express routes:
```js
+// anyone can access public files
router.get('/public/<path:file>', function(req, res) {
res.write(req.params.file);
res.end();
});
+// will match any path that starts with /private
router.all('/private/<path:path>', function(req, res, next) {
if (req.headers['x-user']) {
req.loggedIn = true;
@@ -105,12 +107,13 @@ router.all('/private/<path:path>', function(req, res, next) {
next();
}
});
-
router.all('/private/<path:path>', function(req, res) {
res.writeHead(401); // not authorized
res.end();
});
+// the next two handlers will only be executed if the user is
+// authorized(in this case, the request must have x-user header)
router.post('/private/addpost/<title>', function(req, res) {
// req.loggedIn === true
res.write('post added'));
@@ -129,6 +132,9 @@ router.get('/private/posts', function(req, res) {
```js
router.get(/^\/posts\/(\d+)/i, function(req, res) {
// Will match /posts/5 or /POSTs/32422
+ // captured text can be accessed by index on req.params
+ console.log('Id:', req.params[0])
+ res.end()
})
```
Please sign in to comment.
Something went wrong with that request. Please try again.