Search code examples
node.jsresponse

NodeJS console.log works but response.write doesn't


I'm new to NodeJS.

I am reading this book to learn it.

I am using the following code in one of the request handler modules. Basically, When I get a request from the browser, I send a call to this function and get the contents of the "DIR" command in windows displayed on the screen.

The index.js file is the one which I run as the webapp

This is index.js

var server = require("./server.js");
var router = require("./router.js");
var requestHandlers = require("./requestHandlers");

var handle = {};
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;

server.start(router.route, handle);

This is server.js The server file sends the requests to the router

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

function start(route, handle)
{
function onRequest(request, response)
{
    //console.log("Request recieved!");
    var pathname = url.parse(request.url).pathname;
    console.log("Request for " + pathname + " has been recieved");
    response.writeHead(200, {"Content-Type": "text/plain"});

    route(handle, pathname, response);

    response.end();
}

http.createServer(onRequest).listen(8888);

console.log("Server started!");

}

exports.start = start;

This is router.js This is the router which uses the handle created in index.js (used to map the request to their respective handlers).

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

And finally this is where the requests are being handled, in the requestHandlers.js Over here, It looks like the console.log statement works fine, but the response.write statements don't.

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

function start(response)
{
    console.log("Request handler 'start' was called.");

    exec("dir", function(error, stdout, stderr) {
        response.writeHead(200,{"Content Type": "text/plain"});
        response.write("Hello Upload!");
        response.write(stdout);
        response.write("Hello!");
        console.log(stdout);
        response.end();
    });
}

function upload(response)
{
console.log("Request handler 'upload' was called.");
response.writeHead(200,{"Content Type": "text/plain"});
response.write("Hello Upload!");
response.end();
}

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

Any help would be greatly appreciated


Solution

  • I think the reason your request is breaking is that it looks like response.writeHead is being called twice, once in server#onRequest and once in requestHandlers#upload.

    Node.js Docs - response.writeHead

    Once you're feeling a little more comfortable with Node I'd probably recommend pulling any response writing/ending code out of server.js and implementing a middleware approach where each request gets passed through each requestHandler until it is matched or gets to the end.

    hope that helps -- happy noding!

    EDIT: +1 Andreas' comment. Not the problem on the upload route since that is all synchronous, but in requestHandlers#start once exec is called the control flow continues back to server.js where response.end() is called.