šŸš€ Big News: Socket Acquires Coana to Bring Reachability Analysis to Every Appsec Team.Learn more →
Socket
Book a DemoInstallSign in
Socket

express-route-versioning

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

express-route-versioning

Express middleware that re-routes requests based on HTTP header value

1.5.0
latest
Source
npm
Version published
Weekly downloads
57
78.13%
Maintainers
1
Weekly downloads
Ā 
Created
Source

#express-route-versioning A Connect style middleware to re-direct the execution flow into multiple branches based on HTTP header values. Tested and used for REST API versioning with Express routes but other applications might be suiting.

##Features Customize the extraction of the version via version.use(options) for:

  • any HTTP header using header, i.e. version.use({header: 'Accept-Version'})
  • any HTTP header value using RegEx capture groups in grab, i.e. version.use({grab: /vnd.my.company.()})

Customize the HTTP status code for a failed re-routing using the error, i.e. version.use({error: 404})

##Example Re-route based on version in HTTP header Accept: vnd.mycompany.com+json; version=1.

var express = require('express');
    version = require('express-version-reroute');
version.use({
    'header': 'accept',
    'grab': /vnd.mycompany.com\+json; version=(\d+)(,|$)/,
    'error': 406,
});
var router = express.Router()
  .all('/ping',
       version.reroute({
         1: function(req, res, next) { res.json('pong'); },
         2: function(req, res, next) { res.status(200).end(); },
       })
   );
express().use(router).listen(5000, function() {});

cURL examples that give different behavior based on version.

āžœ  ~  curl -i -X GET 'http://127.0.0.1:5000/ping' -H 'Accept: vnd.mycompany.com+json; version=2'
HTTP/1.1 200 OK
X-Powered-By: Express
Date: Wed, 05 Nov 2014 10:09:50 GMT
Connection: keep-alive
Transfer-Encoding: chunked

āžœ  ~  curl -i -X GET 'http://127.0.0.1:5000/ping' -H 'Accept: vnd.mycompany.com+json; version=1'
HTTP/1.1 200 OK
X-Powered-By: Express
Content-Type: application/json; charset=utf-8
Content-Length: 6
ETag: W/"6-dca458aa"
Date: Wed, 05 Nov 2014 10:09:56 GMT
Connection: keep-alive

"pong"%
āžœ  ~  curl -i -X GET 'http://127.0.0.1:5000/ping' -H 'Accept: vnd.mycompany.com+json; version=0'
HTTP/1.1 406 Not Acceptable
X-Powered-By: Express
Date: Wed, 05 Nov 2014 10:09:58 GMT
Connection: keep-alive
Transfer-Encoding: chunked

Development

npm install
npm test

Keywords

express

FAQs

Package last updated on 18 May 2015

Did you know?

Socket

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.

Install

Related posts