express-uri-template

Expand express.js style URI templates, similar to RFC 6570

express-uri-template

express-uri-template is the reverse of express' application routing.

express:

/forum/:fid/thread/:tid + /forum/hello/thread/world = {"fid": "hello", "tid": "world"}

express-uri-template:

/forum/:fid/thread/:tid + {"fid": "hello", "tid": "world"} = /forum/hello/thread/world

So you can generate urls from templates.

You can use it in the browser, e.g. to generate URLs for AJAX requests or links inserted into the DOM. You can also use it in Node.js, to generate URLs for HTTP redirects or links in generated HTML.

Node.js:

npm install express-uri-template

Bower:

bower install express-uri-template

Use object params:

var eut = require('express-uri-template');
 
var uri = eut('/forum/:fid/thread/:tid', {"fid": "hello", "tid": "world"});
console.log(uri); // -> /forum/hello/thread/world 

Use Array params (aka req.params):

var eut = require('express-uri-template');
 
var uri = eut('/forum/*/thread/*', ["hello", "world"]);
console.log(uri); // -> /forum/hello/thread/world 

This library is inspired by RFC 6570, except that it uses express' uri template syntax, and is much simpler.