Research
Recent Trends in Malicious Packages Targeting Discord
The Socket research team breaks down a sampling of malicious packages that download and execute files, among other suspicious behaviors, targeting the popular Discord platform.
restify-router
Advanced tools
Readme
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.
$ npm install --save restify-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);
});
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:
/users
/users/:id
Posts:
/posts
/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);
});
To prefix all routes, specify the prefix as the second argument to router.applyRoutes(server, prefix)
prefix
must be a string or a regexExample:
Routes:
/admin/settings
/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);
});
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);
// 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.
As an alternative to Nesting Routers, you can use the group to clarify the middlewares manipulation and the routes / files organization. Works in a way that does not need to create multiple instances of the Router like Nesting.
To group routers use the .group
method on a Router:
router.group(path, callback);
var Router = require('restify-router').Router;
var restify = require('restify');
var routerInstance = new Router();
var server = restify.createServer();
routerInstance.get('/', function (req, res, next) {
res.send({message: 'home'});
return next();
});
routerInstance.group('/v1', function (router) {
router.get('/', function (req, res, next) {
res.send({message: 'home V1'});
return next();
});
router.group('/auth', function (router) {
router.post('/register', function (req, res, next) {
res.send({message: 'success (v1)'});
return next();
});
});
});
routerInstance.group('/v2', function (router) {
router.get('/', function (req, res, next) {
res.send({message: 'home V2'});
return next();
});
});
// add all routes registered in the router to this server instance
routerInstance.applyRoutes(server);
server.listen(8081, function() {
console.log('%s listening at %s', server.name, server.url);
});
With the above code definition we can do the following calls:
/
/v1
/v1/auth/register
/v2
var Router = require('restify-router').Router;
var restify = require('restify');
var routerInstance = new Router();
var server = restify.createServer();
function midFirst(req, res, next) { /**/ }
function midSecond(req, res, next) { /**/ }
function midThird(req, res, next) { /**/ }
routerInstance.group('/v1', midFirst, function (router) {
router.get('/', function (req, res, next) {
res.send({message: 'home V1'});
return next();
});
router.group('/auth', midSecond, function (router) {
router.post('/register', midThird, function (req, res, next) {
res.send({message: 'success (v1)'});
return next();
});
});
});
// add all routes registered in the router to this server instance
routerInstance.applyRoutes(server);
server.listen(8081, function() {
console.log('%s listening at %s', server.name, server.url);
});
With the above code definition we can do the following calls:
/v1 [midFirst]
/v1/auth/register [midFirst, midSecond, midThird]
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
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
For more information about Restify Router see Organizing Restify Routes with Restify Router
FAQs
A router interface for restify that lets you aggregate route definitions and apply to a restify server
The npm package restify-router receives a total of 2,418 weekly downloads. As such, restify-router popularity was classified as popular.
We found that restify-router demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
The Socket research team breaks down a sampling of malicious packages that download and execute files, among other suspicious behaviors, targeting the popular Discord platform.
Security News
Socket CEO Feross Aboukhadijeh joins a16z partners to discuss how modern, sophisticated supply chain attacks require AI-driven defenses and explore the challenges and solutions in leveraging AI for threat detection early in the development life cycle.
Security News
NIST's new AI Risk Management Framework aims to enhance the security and reliability of generative AI systems and address the unique challenges of malicious AI exploits.