Search code examples
javascriptnode.jspostdata

Node.js server just keeps loading with no result.


I have been trying to get my server to work but when I send post data it just keeps loading and no results are given. Here is my noen.js file.

var http = require('http');
var url = require('url');

// Configure our HTTP server to respond with Hello World to all requests.
var server = http.createServer(function (request, response) {
  var queryData = url.parse(request.url, true).query;
  response.writeHead(200, {"Content-Type": "text/plain"});

  if (queryData.name) {
    // user told us their name in the GET request, ex: http://host:8000/?name=Tom
    var exec = require('child_process').exec;
    function puts(error, stdout, stderr) {sys.puts(stdout)}
    exec ("casperjs test.js " + queryData.name + '\n');

  } else {
response.end("Contact Admin - Not Working\n");
  }
});

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(1213);

Can anyone help me fix this? When I go to

127.0.0.1:8000/?name=tom 

I get no response the page just goes into a long loading loop


Solution

  • There is no response.end in case if is true so then response "never" ends. write at bottom of the if

    response.end("something");
    

    And you will get the response;

    For get the output of the process to the response:

    https://stackoverflow.com/a/3944751/3018595

    var http = require('http');
    var url = require('url');
    
    // Configure our HTTP server to respond with Hello World to all requests.
    var server = http.createServer(function (request, response) {
      var queryData = url.parse(request.url, true).query;
      response.writeHead(200, {"Content-Type": "text/plain"});
    
      if (queryData.name) {
        // user told us their name in the GET request, ex: http://host:8000/?name=Tom
        var exec = require('child_process').exec;
    
        exec ("casperjs test.js " + queryData.name + '\n',function(err, stdout, stderr) {
    
            response.end(stdout);
    
        });
    
      } else {
        response.end("Contact Admin - Not Working\n");
      }
    });
    
    // Listen on port 8000, IP defaults to 127.0.0.1
    server.listen(1213);