At the very beginning, we need to figure out what node will be blocking.
Node is so advertised:" in node in addition to the code, everything is executed in parallel!" "
This means that node. js can still process tasks in parallel without adding additional external lines--node.js is single-threaded.
That is, we start the Web server, listening to the 8888 port of the Start method, is single-threaded.
If a request is time-consuming, then the subsequent request is not executed until the last request is completed, which is obviously unreasonable!
As in Requesthandlers start handler:
function start () { console.log ("Request handler ' start ' was called.") ); function Sleep (milliSeconds) { varnew Date (). GetTime (); while (new Date (). GetTime () < StartTime + milliSeconds); } Sleep (10000); return "Hello Start";}
We can use the child_process module to implement non-blocking operations, which is actually an asynchronous operation, emphasizing that time-consuming operations usually need to be handled by asynchronous operations.
A model of error:
varexec = require ("Child_process"). exec;functionstart () {Console.log ("Request handler ' start ' was called."); varContent = "Empty"; EXEC ("Ls-lah",function(Error, stdout, stderr) {content=stdout; }); returncontent;}functionupload () {Console.log ("Request handler ' upload ' was called."); return"Hello Upload";} Exports.start=Start;exports.upload= upload;
The cause of the error, the exec asynchronous operation can no longer follow the synchronization code, a simple example, juqery Ajax request after the success of the subsequent operations should be processed in success, and should not be the Ajax entire code block after processing.
Since the subsequent operations are implemented in the asynchronous callback function, the processing of response will be moved to handler implementation.
Server.js
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." = start;
Router.js
function Route (handle, pathname, response) { Console.log ("about-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 (); = route;
Requesthandler.js
VarExec=Require("Child_process").Exec;
functionStart(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();
});
}
functionUpload(Response) {
Console.Log("Request handler ' upload ' was called.");
Response.Writehead(200, { "Content-type" Span class= "pun" >: "Text/plain" });
Response. Write "Hello Upload" Response. End }
Exports. Start = Start;
Exports. Upload = Upload
Nodejs Getting Started learning note two-solving blocking problems