Easily create Nodejs servers (9): Implement Non-blocking operations _node.js

Source: Internet
Author: User

We want the response object (obtained from the server's callback function onrequest ()) to be passed to the request handler via a request route. The handler can then take a function on the object to respond to the request.

Let's make a change to Server.js:

Copy Code code as follows:

var http = require ("http");
var url = require ("url");
function start (route, handle) {
function onrequest (request, Response) {
var pathname = Url.parse (request.url). Pathname;
Console.log ("Request for" + Pathname + "received.");
Route (handle, pathname, response);
}
Http.createserver (ONrequest). Listen (8888);
Console.log ("Server has started.");
}
Exports.start = start;

We pass the response object as the third argument to the route () function, and we remove all the response-related functions in the onrequest () handler because we want this part of the work to be done by the route () function.

Next Modify Router.js:

Copy Code code as follows:

function route (handle, pathname, response) {
Console.log ("About to route a request for" + 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;

The same pattern: instead of getting the return value from the request handler earlier this time, the response object is passed directly. If there is no corresponding request processor processing, we return the "404" error directly.

Next Modify Requesthandler.js:

Copy Code code as follows:

var exec = require ("child_process"). exec;
function Start (response) {
Console.log ("Request handler ' start ' was called.");
EXEC ("Ls-lah", function (Error, stdout, stderr) {
Response.writehead ({"Content-type": "Text/plain"});
Response.Write (stdout);
Response.End ();
});
}

function upload (response) {
Console.log ("Request handler ' upload ' was called.");
Response.writehead ({"Content-type": "Text/plain"});
Response.Write ("Hello Upload");
Response.End ();
}

Exports.start = start;
Exports.upload = upload;

Our handler functions need to receive response parameters in order to respond directly to the request. The start handler does a request-response operation in the anonymous callback function of exec (), and the upload handler is still a simple reply to "Hello world", only this time using the response object.

If you want to prove that the time-consuming operation in the/start handler does not block an immediate response to the/upload request, you can modify the Requesthandlers.js to the following form:

Copy Code code as follows:

var exec = require ("child_process"). exec;
function Start (response) {
Console.log ("Request handler ' start ' was called.");
EXEC ("Find/",
{timeout:10000, maxbuffer:20000*1024},
function (Error, stdout, stderr) {
Response.writehead ({"Content-type": "Text/plain"});
Response.Write (stdout);
Response.End ();
}
);
}

function upload (response) {
Console.log ("Request handler ' upload ' was called.");
Response.writehead ({"Content-type": "Text/plain"});
Response.Write ("Hello Upload");
Response.End ();
}

Exports.start = start;
Exports.upload = upload;

This way, when the request is Http://localhost:8888/start, it takes 10 seconds to load, and when the request Http://localhost:8888/upload, it responds immediately, even at this time The start response is still in process.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.