1

during my studies i have to create a nodejs webserver environment including 2 nodejs webserver and 1 nodejs loadbalancer via http-proxy (module). Therefore i built 2 instances of node js listening to port 8081 and 8082. Example Server A (localhost:8081)

var args = process.argv.splice(2);
var http = require('http');

function whoareU() {
    var n = 'A';
    return n;
}

var server = http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type' : 'text/plain'});
    res.end('Server: ' + whoareU());
});

server.listen(args[0] || 8081);

... ServerB is similar, just changed Port 8082 and Identifier B.

On the second hand i built a "loadbalace" instance via http-proxy listening to port 8080

var http = require('http'),
httpProxy = require('http-proxy');

 var servers =  [{host :'127.0.0.1', port :8081}, {host : '127.0.0.1',port :8082}];

  httpProxy.createServer(function (req, res, proxy) {

  var target = servers.shift();
  proxy.proxyRequest(req, res, target);
  servers.push(target);


   }).listen(8000);

MY INTENTION: If i open the URL 127.0.0.1:8080 i wanna see changing Outputs. "Server: A" or "Server: B". BUT IT DOESNT WORK!!!!!!!!! The message:

Error: Must provide a proper URL as target
    at ProxyServer.<anonymous> (D:\nodejsmain\node_modules\http-proxy\lib\http-p
roxy\index.js:68:35)
    at Server.closure (D:\nodejsmain\node_modules\http-proxy\lib\http-proxy\inde
x.js:125:43)
    at Server.emit (events.js:98:17)
    at HTTPParser.parser.onIncoming (http.js:2112:12)
    at HTTPParser.parserOnHeadersComplete [as onHeadersComplete] (http.js:121:23
)
    at Socket.socket.ondata (http.js:1970:22)
    at TCP.onread (net.js:527:27)

Question A: Why? ;-) By the way: the output of 127.0.0.1:8081 or 127.0.0.1:8082 works fine!

In addition to that one further question (Question B) to my code: How can i understand args[0] in all config-files (server.listen(args[0] || 8081);) ... Before i did more easy configurations like the following sniped shows and i didnt need it.

var http = require('http');
http.createServer(function (request, response) {
 response.writeHead(200,
  {'content-type': 'text/plain; charset=utf-8'});
 response.write('Huhu ');
 response.end('Stefan\n');
}).listen(8080, '127.0.0.1');

So thank u for helping me in both questions (A+B).

1 Answers1

2

For question A, the error message is

Error: Must provide a proper URL as target
at ProxyServer.<anonymous> (D:\nodejsmain\node_modules\http-proxy\lib\http-proxy\index.js:68:35)

That means, you pass a wrong parameter to http-proxy module. Just check http-proxy document.

Then you'd better change your proxy code into this:

var http = require('http'),
httpProxy = require('http-proxy');
var servers =  ['http://127.0.0.1:8002', 'http://127.0.0.1:8003', 'http://127.0.0.1:8002'];

var proxy = httpProxy.createProxyServer();
var count = 0;
http.createServer(function(req,res){
    loadBalanceProxy(req,res);
}).listen(8000);

var currentServer = 1;
function loadBalanceProxy(req, res){
    var cur = currentServer%servers.length;
    currentServer++;
    var target = servers[cur];
    proxy.web(req, res, {
        target: target
    });
}

P.S When I try this code, if I just set server into this:

    var servers =  ['http://127.0.0.1:8002', 'http://127.0.0.1:8003'];

Then I open webpage with

http://127.0.0.1:8000

it will always be 'ServerA'.

Cause in your browser, it will send another request to get '/favicon.ico' from your server. That means Your http server will handle a 'simple text' page with two times request.

Here is relative Question: "nodejs - http.createServer seems to call twice"

For Question B: args equals to process.argv.splice(2)

var args = process.argv.splice(2);

So check the description of process.argv in nodejs document:

"An array containing the command line arguments. The first element will be 'node', the second element will be the name of the JavaScript file. The next elements will be any additional command line arguments."

When you enter this command in terminal:

node server.js test test2

Here is your process.argv:

argv[0] => node
argv[1] => you/path/to/server.js
argv[2] => test
argv[3] => test2 

Then your args means

['test', 'test2']

Enjoy it: )

Community
  • 1
  • 1
Tyler.z.yang
  • 2,402
  • 1
  • 18
  • 31