You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
varexpress=require('express')varrocky=require('rocky')varversion=require('http-version')// Configure rocky proxiesvaroldAPIProxy=rocky()oldAPIProxy.forward('https://localhost:3001')oldAPIProxy.all('/*')varnewAPIProxy=rocky()newAPIProxy.forward('https://localhost:3002')newAPIProxy.all('/*')// create main appvarapp=express()// Configure the middlewares per specific versionapp.use(version('1.0',oldAPIProxy.middleware()))app.use(version('2.0',newAPIProxy.middleware()))// Start the serverapp.listen(3000)// Test target serversvaroldAPIServer=express()oldAPIServer.use(function(res,res){res.end('Hello from old API')})oldAPIServer.listen(3001)varnewAPIServer=express()newAPIServer.use(function(res,res){res.end('Hello from new API')})newAPIServer.listen(3002)
Test it!
varrequest=require('supertest')request('https://localhost:3000').get('/test').set('Version','1.0').expect(200,'Hello from old API').end(function(err){if(err){returnconsole.error('Oops:',err)}console.log('Old API server success')})request('https://localhost:3000').get('/test').set('Version','2.0').expect(200,'Hello from new API').end(function(err){if(err){returnconsole.error('Oops:',err)}console.log('New API server success')})
About
HTTP API version based routing middleware using rocky + express + http-version