I just started going through the node.js beginners guide
And wanted to know if there were any examples or ways I can experience first hand if a blocking operation on a single request would block other requests from starting. Essentially I want to open up two different browsers and trigger a blocking operation from one browser, then access another browser and observe that I am being blocked until the first request finishes.
Also for this code snippet
var http = require("http");
function onRequest(request, response) {
response.write("Hello World");
response.end();
}
http.createServer(onRequest).listen(8888);
Shouldn't the onRequest function accept arguments if it is going to called?
http.createServer(onRequest(request, response)).listen(8888);
For the question in the title, yes. You can try it by, say, having something like this:
function onRequest(req, res) {
if(req.url === '/block') {
while(true) {}
}else{
res.end("Still accepting requests…");
}
}
Then go to http://localhost:8888/block in one browser and http://localhost:8888/ in the other. The latter request will not yield a response. (Neither will the first one, for that matter.)
For your other question, no; you're not calling onRequest and passing the return value to createServer, but rather passing the function itself to createServer.
In JavaScript, functions are first-class, which is to say they're an object like any other: you can pass them as arguments, put them in variables or in other objects, etc. If you're used to C you can think of it like a function pointer except possibly with an implicit context argument pulled along with it to carry references to the variables of its enclosing lexical scopes.