0

I am currently doing a project to automatically spawn new servers dynamically when the current server is overloaded. I am using node.js for creating server. I have also calculated the number of requests using a variable. Is this procedure right? Or should I change my code?

I used a function to spawn a server with arguments server name and port number but I am getting an error in the port number which I have specified. The error is process.nextTick error. The error is on the port number in the function call.

I need a correct procedure to pass port number as arguments to the function.

code

var http = require('http');
var fs = require('fs');
var file = fs.readFileSync('file.html');
var reqno = 1;

function spawnserver(servername, port) {
    servername = http.createServer(function (req, res) {
        res.writeHeader(200, {
            "Content-Type": "text/html"
        });
        res.end(file);
    });
    servername.listen(port);
}
var server = http.createServer(function (req, res) {
    res.writeHeader(200, {
        "Content-Type": "text/html"
    });
    res.end(file);
    reqno++;
});
server.listen(3000);
if (reqno > 200) {
    function spawnserver(server2, 9615);
}

error

vishnu@vishnu-VirtualBox:~$ node server.js

/home/vishnu/server.js:21 function spawnserver(servername,9615);
                                ^^^^

node.js:201
        throw e; // process.nextTick error, or 'error' event on first tick
              ^ SyntaxError: Unexpected number
    at Module._compile (module.js:429:25)
    at Object..js (module.js:459:10)
    at Module.load (module.js:348:32)
    at Function._load (module.js:308:12)
    at Array.0 (module.js:479:10)
    at EventEmitter._tickCallback (node.js:192:41)
Ben Fortune
  • 31,623
  • 10
  • 79
  • 80
Priya Samy
  • 41
  • 6

3 Answers3

0

The function keyword is for defining a function, not for executing one. In this line: function spawnserver(server2, 9615);, remove function.

Niabb
  • 139
  • 5
0

Take advantage of node.js parallel programming by using clusters. https://nodejs.org/api/all.html#all_cluster

0

Have you resolved your issues? If you are asking what ways can you pass in port number? I do not know enough of your requirements, e.g. do you know port number before hand? or it is also dynamically assigned. How many new servers are you doing to spawn? Just one?

One of the ways is to use config.json to define port number. Then your application can get the port number from the configuration file.

hohoho
  • 220
  • 1
  • 3
  • 12
  • i resolved the above issue. But i have another doubt like how to redirect requests among available server instances? @hohoho – Priya Samy Apr 16 '15 at 15:27
  • Just in case somebody come to this 'redirect request' question : Use any http loadbalancer (i.e nginx in reverse proxy mode) in front of your nodeJS server. – Bino Oetomo Jul 07 '18 at 01:47