-1

Why cant send given POST data as response? When write response.write('Data is: ', data.toString()); i expect see data from post in browser, but its not work, why? if i use response.write('Some string'); not in callback request.on('data',...) i see written data in browser. If i use console.log(data) in callback request.on('data',...) i see data in console. But response not given any data in that callback. Why?

var server = require('http').createServer().listen(8080);
var template = '<form action="" method="post"><input placeholder="login" type="text" name="login" /><br><input type="password" placeholder="password" name="password" /><br><input type="submit" /></form>';

server.on('request', function(request, response){
    response.writeHead(200,{
        'Content-Type': 'text/html;charset=utf-8'
    });

    request.on('data', function(data){
        //console.log(data.toString());//it`s work, i see data in console
        response.write('Data is: '+ data.toString()); //but that code is not working. that data not append to response
    });

    response.write(template);
    response.end();
});
  • signature of `response.write` is `response.write(chunk[, encoding][, callback])` so you are providing `data.toString()` as 2nd argument instead of concatenating the string. – Ejaz May 02 '15 at 20:16
  • you are ending the server response before the request's data event fires. you need to wait for `request.on("end")` and call `response.end();` from inside that. – dandavis May 02 '15 at 20:28
  • possible duplicate of [Why is my variable unaltered after I modify it inside of a function? - Asynchronous code reference](http://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron) – Aaron Dufour May 02 '15 at 22:18

3 Answers3

1

Fix for your problem

var server = require('http').createServer().listen(8080);
var template = '<form action="" method="post"><input placeholder="login" type="text" name="login" /><br><input type="password" placeholder="password" name="password" /><br><input type="submit" /></form>';

server.on('request', function(request, response){
    var postData = '';

    response.writeHead(200,{
        'Content-Type': 'text/html;charset=utf-8'
    });

    request.on('data', function(data){
        postData += data.toString(); // store everything sent with the POST request
    });

    request.on('close', function(){
        response.write(template); // print out the template
        if(postData != '')
            response.write('Data is: ', postData); // if POST data was sent output the data
        response.end(); // close the connection
    });
});`

Explanation

Your problem is that node.js/javascript is an event-driven language based on callbacks. When a new request comes in everything in your request-event-function will be executed. This means that the following will be executed:

  • response.writeHead which simply predefines the status etc.
  • definition of a callback for the data-event of request. This means that whenever some POST data gets into the connection the callback will be executed.
  • But then: You write your template to the client and then you close the connection. By that every further callback of the data-event of request will write your POST data to a closed connection.

As the result nothing will be displayed at your website.

  • Thanks for answer but your code is brake browser and page is not loaded. And you print template only if method is post, but i wanna show template on every request type – Undisputed Shadow May 02 '15 at 20:47
  • this will also return a response on the first data event, which is not always the only data event, which is why you need to wait on "end". – dandavis May 02 '15 at 20:56
0
var server = require('http').createServer().listen(8080);
var template = '<form action="" method="post"><input placeholder="login" type="text" name="login" /><br><input type="password" placeholder="password" name="password" /><br><input type="submit" /></form>';

server.on('request', function(request, response){
    response.writeHead(200,{
        'Content-Type': 'text/html;charset=utf-8'
    });

    request.on('data', function(data){
        response.write('OK');//thats not as response part
        console.log(123);//see that in console
    });

    response.write(template);
    response.end();
});
0

Thansk for all. I solved my problem. That code works for me

var server = require('http').createServer().listen(8080);
var template = '<form action="" method="post"><input placeholder="login" type="text" name="login" /><br><input type="password" placeholder="password" name="password" /><br><input type="submit" /></form>';

server.on('request', function(request, response){
    response.writeHead(200,{
        'Content-Type': 'text/html;charset=utf-8'
    });

    request.on('data', function(data){
        response.write(data.toString());
    });

    response.write(template);
    request.on('end', function(){
        response.end();
    });
});
  • 2
    Please explain what you changed and why. Code-only answers are of limited value. – Gert Arnold May 02 '15 at 21:53
  • Sure. Problem was in that: in this case node.js executing asynchronously and he not wait executing callback for event "data". So node.js simply end request before callback for on('data') executed. I just provide request.on('end') and finish response in this event. As i known callbacks for that event executed only after all request-related operations is done – Undisputed Shadow May 03 '15 at 07:39