sarat
sarat

Reputation: 11160

Node.js: Object translates to undefined in the response object

I have a simple function as defined below

function upload(response, postData) {
    console.log("Received: " + postData);
    response.writeHead(200,{"Content-Type":"text/plain"});
    response.write("You've sent text: " + querystring.parse(postData).text);
    response.end();
}

When I log the data in to console, it shows properly but on writing into response object shows as "undefined". Why this happens? Is there any mistake in my code?

Whole code

route.js

function route(handle, pathname, response, postData) {
    console.log("About to route a request for " + pathname);
    if(typeof(handle[pathname]) === 'function') {
        console.log(postData);
        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;

requestHandler.js

var exec = require("child_process").exec;
var querystring = require("querystring");

function start(response, postData) {
    console.log("request handler 'start' was called");
    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) {
    console.log("Received: " + querystring.parse(postData).text);
    response.writeHead(200,{"Content-Type":"text/plain"});
    response.write("You've sent text: " + querystring.parse(postData).text);
    response.end();
}

exports.start = start;
exports.upload = upload;

server.js

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 received for " + pathname + " received.");
        request.setEncoding("utf8");
        request.addListener("data", function(postDataChunk) {
            postData += postDataChunk;
            console.log("Received POST data chunk '" + postDataChunk + "'.");
            });
        request.addListener("end", function() {
            console.log(postData);
            route(handle, pathname, response, postData);
            });
        route(handle,pathname,response);
    }

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

exports.start = start;

Upvotes: 4

Views: 1636

Answers (3)

Blind Owl
Blind Owl

Reputation: 21

This is obviously kinda late, but I had the same exact error as you, but I was not executing route twice. My mistake was that my postData variable was undefined and as a result it was giving

undefinedtext=sometext+querttest 

This problem is easily fixed my initializing your postData = "". Hope this a helps any future visitor that gets stuck here.

Upvotes: 0

Chris
Chris

Reputation: 11

I think it's a typo - use

querystring.parse(postData).undefinedtext 

for your upload function in requestHandler.js

I'm working through the same tutorial and got stuck here too.

Upvotes: 1

sarat
sarat

Reputation: 11160

The code executes twice with and without postData. Thanks Linus G Theil. I should be better clear about the code before posting.

Upvotes: 1

Related Questions