All Projects → ukayani → Restify Router

ukayani / Restify Router

Licence: mit
A router interface for restify that lets you aggregate route definitions and apply to a restify server

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Restify Router

Url Mapper
Take a URL and map to functions, parsing params
Stars: ✭ 39 (-13.33%)
Mutual labels:  router, routing
Freerouting
Advanced PCB autorouter (finally, no Java installation required)
Stars: ✭ 307 (+582.22%)
Mutual labels:  router, routing
Abstract State Router
Like ui-router, but without all the Angular. The best way to structure a single-page webapp.
Stars: ✭ 288 (+540%)
Mutual labels:  router, routing
gatsby-plugin-dynamic-routes
Creating dynamic routes based on your environment and/or renaming existing routes
Stars: ✭ 14 (-68.89%)
Mutual labels:  router, routing
Storeon Async Router
Asynchronous router for Storeon. It provides possibility for prefetch the data, lazy load, navigation cancellation, and routes modification on the fly.
Stars: ✭ 22 (-51.11%)
Mutual labels:  router, routing
Ffrouter
Powerful and easy-to-use URL routing library in iOS that supports URL Rewrite(强大、易用、支持 URL Rewrite的 iOS 路由库)
Stars: ✭ 263 (+484.44%)
Mutual labels:  router, routing
Cortex
Routing system for WordPress
Stars: ✭ 300 (+566.67%)
Mutual labels:  router, routing
go router
The purpose of the go_router for Flutter is to use declarative routes to reduce complexity, regardless of the platform you're targeting (mobile, web, desktop), handling deep linking from Android, iOS and the web while still allowing an easy-to-use developer experience.
Stars: ✭ 380 (+744.44%)
Mutual labels:  router, routing
Micro Router
🚉 A tiny and functional router for Zeit's Micro
Stars: ✭ 621 (+1280%)
Mutual labels:  router, routing
Router
🛣 Simple Navigation for iOS
Stars: ✭ 438 (+873.33%)
Mutual labels:  router, routing
yew-router
Router extension to yew
Stars: ✭ 27 (-40%)
Mutual labels:  router, routing
Routing
The Routing component maps an HTTP request to a set of configuration variables.
Stars: ✭ 7,080 (+15633.33%)
Mutual labels:  router, routing
react-mobx-router5
React components for routing solution using router5 and mobx
Stars: ✭ 58 (+28.89%)
Mutual labels:  router, routing
Simple Php Router
Simple, fast and yet powerful PHP router that is easy to get integrated and in any project. Heavily inspired by the way Laravel handles routing, with both simplicity and expand-ability in mind.
Stars: ✭ 279 (+520%)
Mutual labels:  router, routing
r5r
ipeagit.github.io/r5r/
Stars: ✭ 90 (+100%)
Mutual labels:  router, routing
Fluro
Fluro is a Flutter routing library that adds flexible routing options like wildcards, named parameters and clear route definitions.
Stars: ✭ 3,372 (+7393.33%)
Mutual labels:  router, routing
CRRouter
A simple and powerful router
Stars: ✭ 54 (+20%)
Mutual labels:  router, routing
OpenBSDFirewall
Simple OpenBSD Home Firewall Config for ALIX Board
Stars: ✭ 41 (-8.89%)
Mutual labels:  router, routing
Graphpath
Graphpath generates an ASCII network diagram from the route table of a Unix/Linux
Stars: ✭ 321 (+613.33%)
Mutual labels:  router, routing
Bidi
Bidirectional URI routing
Stars: ✭ 941 (+1991.11%)
Mutual labels:  router, routing

Restify Router

Build Status

This module allows you to define your routes using a Router interface that is identical to how routes are registered on a restify server. You can then apply the routes to a server instance.

Borrowing from the idea of Express router where you can organize routes by creating multiple routers and applying them to an express server, this component allows you to achieve a similar separation/grouping of route definitions.

Installation

$ npm install --save restify-router

Creating a router

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);
});

Why use it?

When your application starts to contain a lot of routes, you may want to group the definition of routes in separate files rather than registering every route in a single server bootstrap/creation file.

For example, if we have two sets of routes in our application:

Users:

  • GET /users
  • GET /users/:id

Posts:

  • GET /posts
  • GET /posts/:id
var userRouter = require('./user.router'); // return a Router with only user route definitions
var postsRouter = require('./posts.router'); // return a Router with only posts route definitions

var restify = require('restify');
var server = restify.createServer();

// add user routes
userRouter.applyRoutes(server);

// add posts routes
postsRouter.applyRoutes(server);

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

Prefixing Routes

To prefix all routes, specify the prefix as the second argument to router.applyRoutes(server, prefix)

  • prefix must be a string or a regex

Example:

Routes:

  • GET /admin/settings
  • GET /admin/controls
var Router = require('restify-router').Router;
var restify = require('restify');

function settings(req, res, next) {
  res.send('settings');
  next();
}

function controls(req, res, next) {
  res.send('controls');
  next();
}

var routerInstance = new Router();

// add a route like you would on a restify server instance
routerInstance.get('/settings', settings);
routerInstance.get('/controls', controls);

var server = restify.createServer();
// add all routes registered in the router to this server instance with uri prefix 'admin'
routerInstance.applyRoutes(server, '/admin');

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

Nesting Routers

If you are familiar with Express style routers, you have the ability to nest routers under other routers to create a hierarchy of route definitions.

To nest routers use the .add method on a Router:

router.add(path, router);
  • path - a string or regexp path beginning with a forward slash (/)
    • All routes defined in the provided router will be prefixed with this path during registration
  • router - the router instance to nest

Example Usage

// routes/v1/auth.js

const router = new Router();
router.post("/register", function (req, res, next) {
 // do something with req.body
 res.send({status: 'success'});
 return next();
});

module.exports = router;
// routes/v1/routes.js

const router = new Router();
router.add("/auth", require("./auth"));

module.exports = router;
// routes/routes.js

const router = new Router();
router.add("/v1", require("./v1/routes"));

module.exports = router;

With the above router definition from routes/routes.js we can do the following call:

POST /v1/auth/register

This call is possible because we have nested routers two levels deep from the /v1 path.

Common Middleware

There may be times when you want to apply some common middleware to all routes registered with a router. For example, you may want some common authorization middleware for all routes under a specific router.

All middleware registered via .use will be applied before route level middleware.

To stay consistent with the restify server interface, the method on the Router is:

  • .use(middlewareFn, middlewareFn2, ...)
  • .use([middlewareFn, middlewareFn2, ...])

Note: Multiple calls to .use will result in aggregation of middleware, each successive call will append to the list of common middleware

Example Usage

var router = new Router();

// this will run before every route on this router
router.use(function (req, res, next) {
   if (req.query.role === 'admin') {
    return next();
   } else {
    return next(new errors.UnauthorizedError());
   }
});

router.get('/hello', function (req, res, next) {
   res.send('Hello');
   next();
});

router.get('/test', function (req, res, next) {
   res.send('Test');
   next();
});

router.applyRoutes(server);

// calling GET /hello  runs use middle ware first and then the routes middleware

Links

For more information about Restify Router see Organizing Restify Routes with Restify Router

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].