Node. js development from scratch (4)-add processing functions after Routing

Source: Internet
Author: User
Routing, as its name implies, means that we have different processing methods for different URLs. For example, the business logic for processing/start should be different from that for processing/upload. In the current implementation, the routing process ends in the routing module, and the routing module is not a module that actually takes action on requests... SyntaxHighlighter. all ();

Routing, as its name implies, means that we have different processing methods for different URLs. For example, the "business logic" of processing/start should be different from that of processing/upload.

In the current implementation, the routing process will "end" in the routing module, and the routing module is not really "action" for the request, otherwise, our applications will not be well extended as they become more complex.

For the moment, we call the function as the route target the request handler. Now let's not rush to develop the routing module, because it doesn't make much sense to improve the routing module if the request processing program is not ready.

RequestHandlers 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:

The requestHandlers module adds a placeholder function for each request handler, and then exports these functions as a module method:

RequestHandlers. js

[Javascript]
Function start (){
Console. log ("Request handler 'start' was called .");
}
 
Function upload (){
Console. log ("Request handler 'upload' was called .");
}
 
Exports. start = start;
Exports. upload = upload;

Router Module
Check whether the request handler corresponding to the specified path exists. If yes, call the corresponding function directly.
Router. js

[Javascript]
Function route (handle, pathname ){
Console. log ("About to route a request for" + pathname );
If (typeof handle [pathname] === 'function '){
Handle [pathname] ();
} Else {
Console. log ("No request handler found for" + pathname );
}
}
 
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 pathname = url. parse (request. url). pathname;
Console. log ("Request for" + pathname + "received .");
 
Route (handle, pathname );
// Here we can process different paths
// If (pathname = "") response. writeHead
Response. writeHead (200, {"Content-Type": "text/plain "});
Response. write ("Hello World ");
Response. end ();
}
 
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 ");
// Case sensitive
Var handle = {}
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, you will see the application output corresponding information, which indicates that our HTTP server is already using the routing module, the Request Path is passed to the route, and the corresponding processing function is found for the route:

 

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.