Restify-Gen, a restify api generator

Volkan Nazmi Metin
ITNEXT
Published in
3 min readMar 1, 2018

--

Click here to share this article on LinkedIn »

For the last two projects, we were using Restify for our backend api’s.

To ease our life when starting with restify, I have implemented a generator like express so that the directory structure can be standardized. Today I would like to share this and its usage.

Restify-Gen is a very simple and easy to use npm package which is publicly available via MIT license. I hope you also find it useful for your projects.

Let’s get to its usage. It is nearly a similar approach that express generator uses. So if you are familiar with express than you would already now the usage.

I recommend to install globally.

The directory structure would be as follows after you executed the above command on your shell.

Folder structure

Here by default you will have the main entry for your node application (app.js), a basic bunyan logger and your routes folder which contains a sample route to /api/ path.

The application would run on port 8080 by default. I will come to different command line options to configure this and other definitions when the app is generated.

index.js is containing a good helper for you to reference how to write a route.

Default Router

At routes folder you can define new routes and separate each of your route into different files. Let’s make an example and show you how easy this goes.

I am going to define a simple get for users in a separate file users.js under routes. The content would be something like this;

User route sample

After we implement our simple user route than all you need to do is define this under app.js so that restify server would understand. Below is our sample user route is defined and app.js

Basically go find the home route, require the newly defined route and than add it to the routes with your path you choose.

To start your application go to the application folder created by restify-gen and execute npm install; npm start on your shell.

Lastly lets take a look at the command line options currently available at restify-gen.

Update 05/03/2018 : Cluster mode implementation

Usage options — updated

Now we have another option -c, — clustered. When provided our generator will provide another file at the root folder which implements Node.js’s cluster mode.

This mode will execute node with multiple workers depending on the number of CPU’s at your server.

Clustered mode execution

I hope this package is going to ease your start with restify.

Thanks for reading this far. If you like what you have read clap me and provide your comments.

--

--

Generally Reasonable, Mostly Reachable and Probably Making Sense. Father, Developer and Manager