Easy creation of nodejs server (9): non-blocking operations, nodejs Server

Source: Internet
Author: User

Easy creation of nodejs server (9): non-blocking operations, nodejs Server

We need to pass the response object (obtained from the server's callback function onRequest () to the request handler through the request route. Then, the handler can use the functions on the object to respond to the request.

Modify server. js first:

Copy codeThe Code is 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 to the route () function as the third parameter, and we remove all the response function calls in the onRequest () handler, because we want this part of work to be completed by the route () function.

Next, modify router. js:

Copy codeThe Code is 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;

In the same mode, the response object is directly transmitted instead of obtaining the return value from the request processing program. If no corresponding request processor is processed, We will directly return the "404" error.

Next, modify requestHandler. js:

Copy codeThe Code is as follows:
Var exec = require ("child_process" cmd.exe c;
Function start (response ){
Console. log ("Request handler 'start' was called .");
Exec ("ls-lah", function (error, stdout, stderr ){
Response. writeHead (200, {"Content-Type": "text/plain "});
Response. write (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;

Our handler functions need to receive the response Parameter in order to make a direct response to the request. The start handler performs the request response operation in the anonymous callback function of exec (), while the upload handler still simply replies "Hello World", but this time only uses the response object.

If you want to prove that time-consuming operations in the/start handler do not block the immediate response to the/upload request, you can change requestHandlers. js to the following format:

Copy codeThe Code is as follows:
Var exec = require ("child_process" cmd.exe c;
Function start (response ){
Console. log ("Request handler 'start' was called .");
Exec ("find /",
{Timeout: 10000, maxBuffer: 20000*1024 },
Function (error, stdout, stderr ){
Response. writeHead (200, {"Content-Type": "text/plain "});
Response. write (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;

In this way, when http: // localhost: 8888/start is requested, it takes 10 seconds to load the data. When http: // localhost: 8888/upload will immediately respond, even if the/start response is still being processed.

Related Article

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.