abdelhadi danba
abdelhadi danba

Reputation: 161

Use Proxy With Express middelware in NodeJS

I have in my private network different services, these services are not accessible from the out side (externe).

I'd like that my services be accessible to some users, and this after an authentication process (for this part I use express).

Once the user is authentificared it will be proxiate to the right service, I tried for this the http-proxy module.

enter image description here

Problem: I failed to use correctly http-proxy with express module, and resolve this enigma as wished.

Code: I began by doing this

    // Create app with Express
    var express  = require('express');
    var app      = express();

    // Create a proxy server with http-proxy
    var httpProxy = require('http-proxy');
    var proxy = httpProxy.createProxyServer();

    // Create target params (in the local network)
    var serverOne = {target:'ws://172.17.0.3:80',ws:true};

    // The use of proxy to expose the service
    app.all("/app/", function(req, res) {
        console.log('redirecting to Server1');
        proxy.web(req, res, serverOne);
    })

    // The login part
    .get('/login', function(req, res) {
        res.render('login.ejs');
        console.log('Cherche Login');
    })

    app.listen(8080);

Result:

enter image description here

Can someone help me please to fix this?

Upvotes: 0

Views: 343

Answers (1)

Aiacciu
Aiacciu

Reputation: 11

Try changing var serverOne = {target:'ws://172.17.0.3:80',ws:true};

to var serverOne = {target:'ws://172.1.0.3:80',ws:true};

Also check that from the server 104.155.15.204, you are able to access the 172.0.1.x network

You can have a look at http://expressjs.com/fr/api.html for implementation of proxies on express.

Regards,

Marc

Upvotes: 1

Related Questions