https://github.com/h2non/rocky-version
HTTP API version based routing middleware using rocky + express + http-version
https://github.com/h2non/rocky-version
Last synced: 5 months ago
JSON representation
HTTP API version based routing middleware using rocky + express + http-version
- Host: GitHub
- URL: https://github.com/h2non/rocky-version
- Owner: h2non
- Created: 2015-06-27T07:33:54.000Z (almost 10 years ago)
- Default Branch: master
- Last Pushed: 2015-06-28T14:25:15.000Z (almost 10 years ago)
- Last Synced: 2024-10-18T11:25:52.726Z (8 months ago)
- Language: JavaScript
- Homepage:
- Size: 121 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# rocky-version
HTTP API version based routing middleware using [rocky](https://github.com/h2non/rocky) + [express](https://github.com/strongloop/express) + [http-version](https://github.com/h2non/http-version)
## Usage
```js
var express = require('express')
var rocky = require('rocky')
var version = require('http-version')// Configure rocky proxies
var oldAPIProxy = rocky()
oldAPIProxy.forward('http://localhost:3001')
oldAPIProxy.all('/*')var newAPIProxy = rocky()
newAPIProxy.forward('http://localhost:3002')
newAPIProxy.all('/*')// create main app
var app = express()// Configure the middlewares per specific version
app.use(version('1.0', oldAPIProxy.middleware()))
app.use(version('2.0', newAPIProxy.middleware()))// Start the server
app.listen(3000)// Test target servers
var oldAPIServer = express()
oldAPIServer.use(function (res, res) {
res.end('Hello from old API')
})
oldAPIServer.listen(3001)var newAPIServer = express()
newAPIServer.use(function (res, res) {
res.end('Hello from new API')
})
newAPIServer.listen(3002)
```Test it!
```js
var request = require('supertest')request('http://localhost:3000')
.get('/test')
.set('Version', '1.0')
.expect(200, 'Hello from old API')
.end(function (err) {
if (err) {
return console.error('Oops:', err)
}
console.log('Old API server success')
})request('http://localhost:3000')
.get('/test')
.set('Version', '2.0')
.expect(200, 'Hello from new API')
.end(function (err) {
if (err) {
return console.error('Oops:', err)
}
console.log('New API server success')
})
```