3

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

user3290741
  • 37
  • 1
  • 1
  • 6

2 Answers2

6

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);
Community
  • 1
  • 1
Mardie
  • 1,663
  • 17
  • 27
  • 1
    this does not fix the full problem and you have only copied what srikarg has said in our comments above – user3290741 Feb 16 '14 at 00:50
  • Sorry but I haven't copied it. Your question is ambiguous. If you want to get the output of the casper process to the response you could pipe the casper process stdout stream. http://nodejs.org/api/child_process.html#child_process_child_stdout – Mardie Feb 16 '14 at 01:04
  • that's exactly what I want to do I want to get the response of casperjs could you show me an example of piping the stdout stream – user3290741 Feb 16 '14 at 01:09
  • hows that this is my last part of my script and I am racking my brains learning this code – user3290741 Feb 16 '14 at 01:13
  • sorry I just don't understand how I could do this at the moment if yu could show and example using my code above I would be so greatfull – user3290741 Feb 16 '14 at 01:21
0

The reason your browser is keep waiting because you are not ending your response. You have to call response.end to let your server complete the response otherwise it will keep thinking that the response is not complete yet. I added a line in your if statement and tested your code and it is working perfectly fine.

added line ** response.end("Request processed successfully...\n");**, assuming that you need to display a different message in case your "else" statement. I tested url http://:1213/?name=tom

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');
response.end("Request processed successfully...\n");   


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

// Listen on port 8000, IP defaults to 127.0.0.1
server.listen(1213);
ATHER
  • 3,254
  • 5
  • 40
  • 63