Registry / devops / rest-handler

rest-handler

JSON →
library1.2.17jsnpmunverified

A Node.js module for creating REST-style request handlers with built-in middleware support, route matching with simple path placeholders, and an event system. Version 1.2.17 uses a path-based router and encapsulates req, res, and next into a single 'rest' object, simplifying request handling compared to Express. Routes are matched before any middleware or handlers run, and unmatched routes can be handled via a 'routeNotFound' event. The package is stable but has low release cadence; it is suitable for simple REST APIs where full Express features are not needed.

npm install rest-handler
INSTALL
IMPORT
SIG · REST-HANDLER
R
rest-handler
devopsjavascriptv1.2.17
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

rest-handler
const restHandler = require('rest-handler').create()
import restHandler from 'rest-handler'
Package uses CommonJS; no ES module wrapper. Use require().create() to instantiate.
.create()
const handler = require('rest-handler').create()
const handler = new restHandler()
The module exports a factory function, not a constructor. Call .create() directly.
.addRoute()
handler.addRoute({ path: '/orders/:id', method: 'GET', handler: fn })
handler.addRoute('/orders/:id', fn)
addRoute expects an object with path, method, handler, etc. Do not pass separate arguments.
.before()
handler.before(function(rest) { rest.next() })
handler.use(function(rest, next) { ... })
Middleware is added via .before(), not .use(). Each middleware must call rest.next() to proceed.
.on()
handler.on('routeNotFound', function(req, res) { ... })
handler.on('routeNotFound', function(rest) { ... })
The routeNotFound event provides req and res separately, not the rest object.

Creates a REST handler with a parameterized route and a 404 handler, then starts an HTTP server.

const restHandler = require('rest-handler').create(); restHandler.addRoute({ path: '/api/hello/:name', method: 'GET', handler: function(rest) { const name = rest.params.name || 'World'; rest.res.setHeader('Content-Type', 'text/plain'); rest.res.end('Hello ' + name); } }); restHandler.on('routeNotFound', function(req, res) { res.statusCode = 404; res.end('Not found'); }); const server = require('http').createServer(restHandler); server.listen(3000, () => console.log('Server running on port 3000'));
Debug
Known issues
gotchaMiddleware must call rest.next() to continue; otherwise the request hangs.
fix
Ensure every .before() callback invokes rest.next() once processing is complete.
affects: >=0.0.0
gotchaRoute method defaults to all methods if not specified; omitting method may lead to unintended route matching.
fix
Always specify the HTTP method in addRoute(), or set '*' explicitly if desired.
affects: >=0.0.0
gotchaError status code is optional in rest.error(); calling with one argument uses default 500.
fix
Always pass status code as first argument: rest.error(400, 'Bad request')
affects: >=0.0.0
gotchaThe routeNotFound event receives (req, res) not the rest object, unlike other events.
fix
Use function(req, res) { ... } for routeNotFound event handler.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: restHandler is not a constructor
Calling 'new restHandler()' instead of 'require('rest-handler').create()'.
fix
Use: const restHandler = require('rest-handler').create();
TypeError: rest.next is not a function
Forgetting to include rest parameter in middleware callback, or using wrong signature.
fix
Ensure middleware function signature is correct: function(rest) { rest.next(); }
HTTP 404 for routes that match path but not method
Route method was omitted and unintended method matched, or route method specified incorrectly.
fix
Check addRoute method field: use string like 'GET' or '*'. Omission defaults to all methods.
Upgrade
Version history
1.2.17latest on npm
Audit
Dependencies
path-based-routerrequiredUnderlying router for path matching and parameter extraction
Agent activity
5 hits · last 30 days
node
4
Amazon
1
Resources
rest-handler — npm install rest-handler · libregistry