Question

I have the following code

console.log(typeof(postData));
console.log(typeof(querystring.parse(postData)));
console.log(typeof(querystring.parse(postData).text));
console.log(querystring.parse(postData).text);
var stuff = querystring.parse(postData).text;
console.log(stuff);
stuff = "You've sent the text: " + stuff;
console.log(stuff);
console.log("Request handler 'upload' was called.");
response.writeHead(200, {"Content-Type": "text/plain"});
console.log(response.write(stuff));
response.end();
console.log("More text");
console.log(stuff);

In the console I get

string
object
string
ffffffffffffffffffffffffffffffffffffffffffffffffff
ffffffffffffffffffffffffffffffffffffffffffffffffff
You've sent the text: ffffffffffffffffffffffffffffffffffffffffffffffffff
Request handler 'upload' was called.
false
More text
You've sent the text: ffffffffffffffffffffffffffffffffffffffffffffffffff

But on the webpage I see

You've sent the text: undefined

I can't figure out why my post data is not sending but the string I append it to is.

I am following the tutorial from The Node Beginner Book.

The full requestHandlers code (minus console), where start is the start page and upload is what is returned (the previous code)

var querystring = require("querystring");
function start(response, postData) {
var body = '<html>'+
'<head>'+
'<meta http-equiv="Content-Type" content="text/html; '+
'charset=UTF-8" />'+
'</head>'+
'<body>'+
'<form action="/upload" method="post">'+
'<textarea name="text" rows="20" cols="60"></textarea>'+
'<input type="submit" value="Submit text" />'+
'</form>'+
'</body>'+
'</html>';

response.writeHead(200, {"Content-Type": "text/html"});
response.write(body);
response.end();
}
function upload(response, postData) {
    var stuff = querystring.parse(postData).text;
    stuff = "You've sent the text: " + stuff;
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.end();
}
exports.start = start;
exports.upload = upload;
Was it helpful?

Solution

I think the error is probably not in your requestHandlers.js. Although, in your snippet you've forgot to put in the "response.write("You've sent the text: " + stuff);" line, I'm assuming this is a copy and paste error.

Your start.js file should look like this:

var http = require("http");
var url = require("url");
function start(route, handle) {
    function onRequest(request, response) {
        var postData = "";
        var pathname = url.parse(request.url).pathname;
        console.log("Request for " + pathname + " received.");
        request.setEncoding("utf8");

        request.addListener("data", function (postDataChunk) {
            postData += postDataChunk;
            console.log("Received POST data chunk '" + postDataChunk + "'.");
        });

        request.addListener("end", function () {
            route(handle, pathname, response, postData);
        });

    }

    http.createServer(onRequest).listen(8888);
    console.log("Server has started");
}

exports.start = start;

Note that the route is called in the request objects "end" handler and the postData is built up in "chunks" in the request object's "data" handler.

In your router.js file should be;

function route(handle, pathname, response, postData) {
    console.log("About to route a request for " + pathname);
    if (typeof handle[pathname] === 'function') {
        handle[pathname](response, postData);
    } else {
        console.log("No request handler found for " + pathname);
        response.writeHead(404, {
            "Content-Type" : "text/plain"
        });
        response.write("404 Not found");
        response.end();
    }
}

exports.route = route;

I suppose the key here is to make sure you are passing the postData variable and NOT the request variable. I'm guessing this is what is happening in your code.

This is all on pages 51-53 of the book.

OTHER TIPS

Another fix would be to remove the ".text" at the end of the function. I am using the same tutorial and have run into the same problem. I'm not following the tutorial 100% correctly, but I have found the removing the ".text." and stringifying returned the postData into both the console and the server webpage.

function enterUsers(response, postData)
{
console.log("user entered");
//response.write(query.fullname); 
//response.write("\nuser entered... ;)");
//response.write(postData); 
//response.write(querystring.parse(postData).text)

response.write("\n"+postData)
var receivedData = querystring.parse(postData);
console.log(receivedData); 
response.write("\nYou've sent the text: \n" + querystring.stringify(receivedData, '\n', ' = '));
response.end();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top