问题
I am using strongloop/loopback for the first time. There is some rudimentary instruction online for versioning an API:
var p = require('../package.json');
var version = p.version.split('.').shift();
module.exports = {
restApiRoot: '/api' + (version > 0 ? '/v' + version : ''),
host: process.env.HOST || 'localhost',
port: process.env.PORT || 3000
};
What I don't see, is a recommended way of maintaining multiple versions of an API. So, for example, I could have a legacy 1.4 version and new 2.0 version running at the same time. Is there a best practice for doing so?
Edit: I have accepted @Overdrivr's answer as it is accurate at time of writing. However, there is an open issue on Github regarding this feature with a user planning to submit a PR. Hopefully this functionality will be added in a future iteration.
回答1:
You will need to run two loopback servers, one running 1.4 and the other 2.0. Make one listen on port 3000 and the other on 3001 for instance.
Then you can use nginx as reverse-proxy.
nginx will listen to port 80 or 443 (http or https), the port that your clients will reach.
Finally, tell nginx to re-route requests matching /api/v1.4/
to server 1.4 and /api/v2/
to the other.
来源:https://stackoverflow.com/questions/39883944/strongloop-loopback-maintain-multiple-versions-of-api