简体   繁体   中英

How to use node.js as gate to a website

I got an old website on a webserver. It is to big and not structured well enough but needs to be improved by eg Account management. As it is (in my opinion) at its end of lifetime, we do not want to put more effort in it but instead migrate to new technology. For that, we want to use node.js and AngularJS, because the whole project is more a webapp than it was at the beginning. As a migration concept, we want to include the old stuff via a kind of routing through the node.js server and replace it step by step. For that I looked into the "request" library without getting the right grip. Goal is, to route some requests after authorization check to the old server, without leaving the new server (gate). For that I need to check and parse the gets and posts. Some other requests have to response by the node.js server itself. As I think that I am not the only one with that approach I am asking for experience in that matter.

I had to do something similar, because we made a new API which was not compatible with the first version and some features were not implemented in the newer API so we had to do like a bridge. Authentication was happening in the first server, and then we were routing the request to the old API and then returning to the user.

The approach I took was using a module like request to make the call in the old server.

Assuming you are using expres for your new API, you can do something similar

var request = require('request');
app.get('/test', function(req, res) {
    //authenticate stuff
    var options = {
        url: 'http://oldendpoint.com/test',
        headers: {
            //headers for authenticate in the old endpoint
        }
    };

    request(options, function (error, response, body) {
        if (!error && response.statusCode == 200) {
            res.send(body); //this will send it back to your client
        }
    });
});

Basically you get a request to your new API (node.js app) in the /test endpoint and this, after auth or after whatever check, will forward the request to the old system, and then it will return some data which is forwarded to the client who made the request in the first place.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM