EquestHandlers Module
Applications need new components, so adding new modules does not need to be new. Create a module called requestHandlers, add a placeholder function for each request handler, and then export these functions as modules:
RequestHandlers Module
Start (): POSt form HTML
Upload: POST Data Acquisition page
RequestHandlers. js
[Javascript]
Function start (response, postData ){
Console. log ("Request handler 'start' was called .");
Var body = ''<Head>' +
'<Meta http-equiv = "Content-Type" content = "text/html;' +
'Charset = UTF-8 "/> '+
'</Head>' +
'<Body>' +
'<Form action = "/upload" method = "post">' +
'<Textarea name = "text" rows = "20" cols = "60"> </textarea>' +
'<Input type = "submit" value = "Submit text"/>' +
'</Form>' +
'</Body>' +
'</Html> ';
Response. writeHead (200, {"Content-Type": "text/html "});
Response. write (body );
Response. end ();
}
Function upload (response, postData ){
Console. log ("Request handler 'upload' was called .");
Response. writeHead (200, {"Content-Type": "text/plain "});
Response. write ("You 've sent:" + postData );
Response. end ();
}
Exports. start = start;
Exports. upload = upload;
Router Module
Check whether the request handler corresponding to the given path exists. If yes, call the corresponding function directly and return the corresponding string
Router. js
[Javascript]
Function route (handle, pathname, response, postData ){
Console. log ("About to route a request for" + pathname );
If (typeof handle [pathname] === 'function '){
Handle [pathname] (response, postData );
} 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;
Server Module
Request Processing Module
Server. js
[Javascript]
Var http = require ("http ");
Var url = require ("url ");
Function start (route, handle ){
Function onRequest (request, response ){
Var postData = "";
Var pathname = url. parse (request. url). pathname;
Console. log ("Request for" + pathname + "received .");
Request. setEncoding ("utf8 ");
Request. addListener ("data", function (postDataChunk ){
PostData + = postDataChunk;
Console. log ("Received POST data chunk '" +
PostDataChunk + "'.");
});
Request. addListener ("end", function (){
Route (handle, pathname, response, postData );
});
}
Http. createServer (onRequest). listen (8888 );
Console. log ("Server has started .");
}
Exports. start = start;
Index module
Startup module, main module
Index. js
[Javascript]
Var server = require ("./server ");
Var router = require ("./router ");
Var requestHandlers = require ("./requestHandlers ");
Var handle = {}
// Case sensitive
Handle ["/"] = requestHandlers. start;
Handle ["/start"] = requestHandlers. start;
Handle ["/upload"] = requestHandlers. upload;
Server. start (router. route, handle );
Effect after running
If you start the application (node index. js, always remember this command line), then request a URL, I request http: // localhost: 8888/, and then display a form, after the data is input and submitted, the page will jump to http: // localhost: 8888/upload. You will see the application output information, this indicates that our HTTP server is already using the routing module, and will pass the Request Path to the route, and then find the corresponding processing function for the route: