Browse Source

Add semver middleware

This uses the http-proxy module to send requests to another API instance
based on the version string contained in the request.
pull/213/head
Julian Simioni 9 years ago
parent
commit
fc6ed1c7f3
  1. 25
      middleware/semver.js
  2. 1
      package.json

25
middleware/semver.js

@ -0,0 +1,25 @@
var httpProxy = require('http-proxy');
var proxy = new httpProxy.createProxyServer();
function setup(peliasConfig) {
var currentVersion = peliasConfig.version; //TODO :add to pelias-config
var middleware = function middleware(req, res, next){
var reqVersion = req.params.vr;
// if URI contains v1, handle locally
if (currentVersion === reqVersion) { // uri contains v1
next();
} else if (peliasConfig.proxyMap.hasOwnProperty(reqVersion)){ // else send to legacy server
var host = peliasConfig.proxyMap[reqVersion];
req.url = req.url.substring(1 + reqVersion.length);
proxy.proxyRequest(req, res, { target: host });
} else {
throw new Error('can\'t handle this version');
}
};
return middleware;
}
module.exports = setup;

1
package.json

@ -41,6 +41,7 @@
"geojson": "^0.2.1",
"geojson-extent": "^0.3.1",
"geopipes-elasticsearch-backend": "^0.2.0",
"http-proxy": "^1.11.1",
"is-object": "^1.0.1",
"markdown": "0.5.0",
"microtime": "1.4.0",

Loading…
Cancel
Save