npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

restify-better-router

v1.0.3

Published

A clean way to organize routes for the restify router

Downloads

7

Readme

A Better Restify Router

A clean way to organize routes for the restify router

Build Status npm version license

Installation

$ npm i -S restify-better-router

Command not working? Follow this tutorial.

Motivation

This package allows you to define your routes using a Route object, making it very easy to attach routes to your restify server. In larger applications which have many routes organizing routes off of file structure may be more optimal than one large file.

Example

File user.js

const Route = require('restify-better-router')

module.exports.name = new Route()
    .get('/name/:name')
    .addHandler(function (req, res, next) {
        res.send('Hello ' + req.params.name)
        next()
    })

File place.js

const Route = require('restify-better-router')

module.exports.name = new Route()
    .get('/place/:place')
    .addHandler(function (req, res, next) {
        res.send('Hello from ' + req.params.place)
        next()
    })

File api.js

const restify = require('restify');
const Route = require('restify-better-router')

var server = restify.createServer()

// sub routes
const name = require('./name')
const place = require('./place')

var api = new Route()
    .setPath('/api')
    .addRoutes([name, place])
    .attach(server)

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

Creating a route

A router object is an isolated instance of routes. The router interface matches the interface for adding routes to a restify server:

var Router = require('restify-router').Router;
var routerInstance = new  Router();
var restify = require('restify');

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

// add a route like you would on a restify server instance
routerInstance.get('/hello/:name', respond);

var server = restify.createServer();
// add all routes registered in the router to this server instance
routerInstance.applyRoutes(server);

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

Usage

Upon creating a new route an object can be given as a shortcut to the chainable methods

new Route({
    name:       {string} Given route name
    version:    {string|string[]} Version require for route
    path:       {string} Path for a route
    method:     {string} GET|POST|PUT|PATCH|DELETE|HEAD|OPTIONS
    handler:    {Function|Function[]} A handler function, or Array of handler functions
})

If you prefer, fluent methods can be used instead of the object. Assuming we create a route object like this var r = new Route().

One can set path and method using...

  • r.setPath(path) Sets the path for route as path
  • r.get(path) Sets GET method, with a specified path
  • r.post(path) Sets POST method, with a specified path
  • r.put(path) Sets PUT method, with a specified path
  • r.patch(path) Sets PATCH method, with a specified path
  • r.head(path) Sets HEAD method, with a specified path
  • r.del(path) or r.delete(path) Sets DELETE method, with a specified path
  • r.opts(path) or r.options(path) Sets OPTIONS method, with a specified path

One can modify the route's other attributes using...

  • r.setName(name) Given route the name name
  • r.setVersion(verion) Sets a route's version to version (can be an array of versions)
  • r.addHandler(handler) Adds handler as the route's function to the route
  • r.addRoute(route) Adds a child route to be prefixed by this route's path
  • r.addRoutes(routes) Adds an array of children routes
  • r.attach(server) Attachés this route and its children to server

Attaching routes to a server

Once you have made and grouped your routes, you can attach them to the restify server to become live.

const Route = require('restify-better-router')
const restify = require('restify')

var r = new Route({
    name: 'route',
    method: 'get',
    handler: function (req, res, next) {
        res.send('Hello World!')
        next()
    }
})

var server = restify.createServer()

// r will now be seen by the server
r.attach(server)

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

Influenced by restify-router.