2way-router
Advanced tools
Comparing version 1.0.3 to 1.0.4
{ | ||
"name": "2way-router", | ||
"version": "1.0.3", | ||
"version": "1.0.4", | ||
"description": "2-way router", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -45,3 +45,3 @@ 2way-router | ||
```bash | ||
npm install 2way-router express | ||
npm install 2way-router express vow | ||
``` | ||
@@ -52,7 +52,45 @@ Example application: | ||
var express = require('express'); | ||
var vow = require('vow'); | ||
var router = new Router(); | ||
var links = [ | ||
{ | ||
page: 'main', | ||
text: 'main page' | ||
}, | ||
{ | ||
page: 'news', | ||
text: 'news page' | ||
}, | ||
{ | ||
page: 'news-archive', | ||
text: 'news archive for today', | ||
params: { | ||
year: new Date().getFullYear(), | ||
month: pad(new Date().getMonth() + 1, 2), | ||
day: pad(new Date().getDate(), 2) | ||
} | ||
} | ||
]; | ||
function pad(value, length) { | ||
value = String(value); | ||
while (value.length < length) { | ||
value = '0' + value; | ||
} | ||
return value; | ||
} | ||
function createPage(name, route) { | ||
function pageController(req, res, params) { | ||
res.send(200, 'page: ' + name + ' + params: ' + JSON.stringify(params)); | ||
var pageContent = 'page: ' + name + ', params: <pre>' + JSON.stringify(params, null, ' ') + '</pre>'; | ||
vow.all(links.map(function (link) { | ||
return router.url(link.page, link.params || {}); | ||
})).then(function (urls) { | ||
pageContent += '<ul>'; | ||
urls.forEach(function (href, index) { | ||
pageContent += '<li><a href="' + href + '">' + links[index].text + '</a></li>'; | ||
}); | ||
pageContent += '</ul>'; | ||
res.send(200, pageContent); | ||
}); | ||
} | ||
@@ -72,11 +110,9 @@ | ||
app.use(function (req, res) { | ||
try { | ||
router.findRoute(req.path).done(function (info) { | ||
info.route.controller()(req, res, info.params); | ||
}); | ||
} catch(error) { | ||
res.send(404, 'Not found'); | ||
} | ||
router.findRoute(req.path).then(function (info) { | ||
info.route.controller()(req, res, info.params); | ||
}, function () { | ||
res.send(404, 'Not found'); | ||
}); | ||
}); | ||
app.listen(8080); | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
32882
116