Restify — Routing. Restify is a simple Node back end…
By: John Au-Yeung
Tags: Programming, Web Development, JavaScript, Software Development, Technology

Photo by Diego Jimenez on Unsplash

Restify is a simple Node back end framework.

In this article, we’ll look at how to add routes with Restify.

The next Function

The next function is called to call the next handler in the chain.

For example, we can write:

var restify = require('restify'); function respond(req, res, next) {

res.send('hello ' + req.params.name);

next();

} var server = restify.createServer(); server.use([

function(req, res, next) {

if (Math.random() < 0.5) {

res.send('done!');

return next(false);

}

return next();

}

]); server.get('/hello/:name', respond);

server.head('/hello/:name', respond); server.listen(8080, function() {

console.log('%s listening at %s', server.name, server.url);

});

Then if Math.random returns a number less than 0.5, then we get that the response is 'done' .

Otherwise, then routes are going to be called.

next also accepts an object that’s where instanceof Error is true .

For instance, we can write:

var restify = require('restify'); function respond(req, res, next) {

res.send('hello ' + req.params.name);

next();

} var server = restify.createServer(); server.use([

function(req, res, next) {

return next(new Error('error'));

}

]); server.get('/hello/:name', respond);

server.head('/hello/:name', respond); server.listen(8080, function() {

console.log('%s listening at %s', server.name, server.url);

});

Then we’ll see the error response since we passed in an Error instance with the message 'error' .

We can also call res.send with an error object.

We can write:

var restify = require('restify'); function respond(req, res, next) {

res.send(new Error('error'));

return next();

} var server = restify.createServer(); server.get('/hello/:name', respond);

server.head('/hello/:name', respond); server.listen(8080, function() {

console.log('%s listening at %s', server.name, server.url);

});

to send an error response.

Routing

The routing logic for Restidy is similar to Express.

HTTP verbs are used with the parameterized resource to determine which handler to run.

The values associated with named placeholders are in req.params .

For example, we can write:

var restify = require('restify'); function send(req, res, next) {

res.send('hello ' + req.params.name);

return next();

} var server = restify.createServer(); server.post('/hello', function(req, res, next) {

res.send(201, Math.random().toString(36).substr(3, 8));

return next();

});

server.put('/hello', send);

server.get('/hello/:name', send);

server.head('/hello/:name', send);

server.del('/hello/:name', function(req, res, next) {

res.send(204);

return next();

}); server.listen(8080, function() {

console.log('%s listening at %s', server.name, server.url);

});

We have multiple routes in our Restify app.

get lets us handle GET requests.

post lets us handle POST requests.

put lets us handle PUT requests.

del lets us handle DELETE requests.

Other verbs that can be specified include opts and patch .

Hypermedia

We can render results of other routes with the server.route.render method.

For example, we can write:

var restify = require('restify'); function send(req, res, next) {

res.send('hello ' + req.params.name);

return next();

} var server = restify.createServer(); server.get({name: 'city', path: '/cities/:slug'}, function(req, res, next) {

res.send(req.params.slug);

return next();

}); server.get('/hello', function(req, res, next) {

res.send({

country: 'Australia',

capital: server.router.render('city', {slug: 'canberra'}, {details: true})

});

return next();

}); server.listen(8080, function() {

console.log('%s listening at %s', server.name, server.url);

});

Then when we go to http://localhost:8080/hello , we get:

{

"country": "Australia",

"capital": "/cities/canberra?details=true"

}

The path and query string parameters will be URL encoded properly.

Conclusion

We can create routes with various server methods with Restify.