jay
jay

Reputation: 33

NodeJs server stops responding after 1 response

    var http = require('http');

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(9000, "127.0.0.1");
console.log('Server running at http://127.0.0.1:9000/');

I have the above code to get started with nodejs, when I start the process and run on a browser I get the response Once, but after that I dont get any response. Everytime I restart I get 1 response and as always it stops. How can I get this is run continuously. Thanks in advance!

Just adding more information related to this issue. Here is a snippet from the nginx conf file

server {

    listen 80;

    client_max_body_size 2M;


    server_name my_domain;


     root /home/node/My_Folder;
   # access_log  /var/log/nginx.vhost.access.log  main;
    send_timeout 1;

location ~* ^.+\.(jpg|jpeg|JPG|JPEG|GIF|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mov|html)$ {
autoindex on;
root /home/node/My_Folder;
expires 30d;

break;
}

location / {
      proxy_set_header  X-Real-IP  $remote_addr;
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
        proxy_redirect off;
        #proxy_connect_timeout 50ms;
        #proxy_send_timeout 200ms;
        #proxy_read_timeout 200ms;
        proxy_next_upstream error;
        proxy_pass http://Handler;
        #index no_ads.html no_ads.htm;
        break;
}

}

upstream Handler {
        server 127.0.0.1:8010;
        server 127.0.0.1:8011;
        server 127.0.0.1:8012;
        server 127.0.0.1:8013;
        server 127.0.0.1:8014;
        server 127.0.0.1:8015;
        server 127.0.0.1:8016;
        server 127.0.0.1:8017;
        server 127.0.0.1:8018;
        server 127.0.0.1:8019;
        server 127.0.0.1:9000;
} 

I tried using both

node app.js forever start -a app.js

to start the app, but either ways I get just one response and then a time-out. I do have a couple of other node apps running on the same server and those seem to be working fine. So I am totally lost

Upvotes: 3

Views: 1719

Answers (1)

Golo Roden
Golo Roden

Reputation: 150624

Your Node.js application runs on port 9000.

Inside your NGinx configuration file, you have the setting

proxy_pass http://Handler;

which shall redirect the incoming requests to the Node.js applicaton, but you are not directly redirecting the requests there, but to an upstream that is configured as follows:

upstream Handler {
    server 127.0.0.1:8010;
    server 127.0.0.1:8011;
    server 127.0.0.1:8012;
    server 127.0.0.1:8013;
    server 127.0.0.1:8014;
    server 127.0.0.1:8015;
    server 127.0.0.1:8016;
    server 127.0.0.1:8017;
    server 127.0.0.1:8018;
    server 127.0.0.1:8019;
    server 127.0.0.1:9000;
}

As NGinx by default uses round-robin for upstreams that means that in one of eleven times NGinx tries to connect to port 9000 (which works), and the next ten times tries to access a server that does not exist.

Hence no connection can be made, and you'll get the error message.

Remove all the other server entries within the upstream block, remove the upstream block entirely and configure the single Node.js server directly as proxy, or start additional Node.js servers using the ports 8010, 8011, ..., and everything should work.

For details on how to configure upstreams, please have a look at the NGinx documentation on the HttpUpstreamModule.

Upvotes: 3

Related Questions