Socket
Socket
Sign inDemoInstall

express-route-versioning

Package Overview
Dependencies
0
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

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


Version published
Weekly downloads
1.1K
increased by27.71%
Maintainers
1
Install size
11.3 kB
Created
Weekly downloads
 

Readme

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

FAQs

Last updated on 18 May 2015

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc