Categories
Restify

Getting Started Back End Development with Restify

Spread the love

Restify is a simple Node back end framework.

In this article, we’ll look at how to create a simple back end app with Restify.

Quick Start

To get started with Restify, we can install the restify package by running:

npm i restify

Then we can create our first app by writing:

var restify = require('restify');

function respond(req, res, next) {
  res.send(`hello ${req.params.name}`);
  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);
});

We import the restify package.

Then we create our respond function with the req , res , and next parameters.

req has the request data.

res lets us create our responses with the methods that it provides.

next is a function to call the next middleware.

Then we created our server with the restify.createServer method.

Then we defined our routes with the get method to define a GET route.

The first argument is the URL.

The :name is the placeholder for the URL parameter.

We can access the parameter value with req.params.name as we did with the respond function.

server.head mounts a chain on the given path against the HTTP verb.

We need it to let us listen to requests.

Now when we go to https://localhost:8000/hello/name, we get:

"hello name"

on the screen.

The URL parameters are automatically mapped according to their position.

Sinatra Style Handler Chains

We can have a chain of route handlers.

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.get('/', function(req, res, next) {
  res.send('home')
  return next();
});

server.post('/foo',
  function(req, res, next) {
    req.someData = 'foo';
    return next();
  },
  function(req, res, next) {
    res.send(req.someData);
    return next();
  }
);

server.listen(8080, function() {
  console.log('%s listening at %s', server.name, server.url);
});

to add a get and post route.

The POST route has 2 middlewares.

We call next so that the next one can be called.

So when we make a POST request to https://localhost:8080/foo , we see 'foo' is the response.

There are 3 distinct handler chains.

pre is a handler chain run prior to routing.

use is a handler chain run after routing.

And {httpVerb} is a handler chain that’s run specific to a route.

We can run a pre middleware by writing:

var restify = require('restify');

function respond(req, res, next) {
  res.send(`hello ${req.params.name}`);
  next();
}

var server = restify.createServer();
server.pre(restify.plugins.pre.dedupeSlashes());

server.get('/', function(req, res, next) {
  res.send('home')
  return next();
});

server.post('/foo',
  function(req, res, next) {
    req.someData = 'foo';
    return next();
  },
  function(req, res, next) {
    res.send(req.someData);
    return next();
  }
);

server.listen(8080, function() {
  console.log('%s listening at %s', server.name, server.url);
});

We run the dedupeSlashes middleware before anything else is run to remove extra slashes in our requests.

The server.use handler is run prior to running a route handler.

To use it, 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) {
  console.warn('run for all routes!');
  return next();
});
server.get('/', function(req, res, next) {
  res.send('home')
  return next();
});

server.post('/foo',
  function(req, res, next) {
    req.someData = 'foo';
    return next();
  },
  function(req, res, next) {
    res.send(req.someData);
    return next();
  }
);

server.listen(8080, function() {
  console.log('%s listening at %s', server.name, server.url);
});

Then the use handler will be run before a route handler is run.

Conclusion

We can create simple back end apps with Restify with ease.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *