← helloexpress

routes

8fd9cff65f36958d0691231fafdbd31e44ea757e · ovidiucs · Apr 17, 2014 18:25

+12 −3 · 1 file changed

  • app.js +12 −3
diff --git a/app.js b/app.js
index d97e075..9818e18 100644
--- a/app.js
+++ b/app.js
@@ -22,10 +22,19 @@ app.get("/", function (req,res) {
 });
 
 var newlist = ['apple','orange','beer','banana','strawberry','bread'];
+
+app.param('from', function (req, res, next, from) {
+   req.from = parseInt(from, 10)
+   next();
+});
+
+app.param('to', function (req, res, next, to) {
+   req.to = parseInt(to, 10)
+   next();
+});
+
 app.get("/users/:from-:to", function (req, res) {
-   var from = parseInt(req.params.from, 10),
-       to   = parseInt(req.params.to, 10);
-       res.json(newlist.slice(from, to + 1));
+       res.json(newlist.slice(req.from, req.to + 1));
 }); 
 
 http.createServer(app).listen(app.get("port"), function(){