1

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

4
  • 2
    We'll need A) a complete example. The snippet above looks OK but we need to see the whole picture. And B) accurate, technical description of the program's behavior as opposed to "it doesn't work". What does it do? What are you expecting? Are there error messages? Commented Jul 13, 2013 at 7:42
  • 1
    +1 post the full code. tell what its currently doing & what it is supposed to do Commented Jul 13, 2013 at 7:51
  • There, I've made the changes as requested. :) Commented Jul 13, 2013 at 12:46
  • In onRequest you call response.end(), and since requestHandlers.start() is asynchronous, .end() is called before .write(). (Edit: to be more specific, exec() is async) Commented Jul 13, 2013 at 16:21

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help! I just commented the response.writeHead() and response.end() part from server.js

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.