Create node. js server easily (5): event handler _ node. js-js tutorial

Source: Internet
Author: User
This article mainly introduces how to easily create a node. js server (5): event processing program. This series of articles will create a complete node. js server step by step, if you need it, you can refer to the introduction of an event processor module to provide different feedback for different requests.

This module is named requestHandlers. We first add the placeholder functions start () and upload.

The requestHandlers. js code is as follows:

The Code is as follows:


Function start (){
Console. log ("this is called when accessing/star. ");
}

Function upload (){
Console. log ("called when accessing/upload. ");
}

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

In real applications, the number of request handlers increases constantly. We certainly do not want to complete the request in the route every time a new URL or request handler is created.

To the processing program ing and repeatedly tossing.

In addition, we do not want to have a lot of if request = x then call handler y in the route, which will make the Code look messy and unprofessional.

Here I will use the concept of correlated array to deal with this requirement. We will pass a series of request handlers through an object, and we need to use loosely coupled method to inject this object into route () function.

We will first introduce this object to the index. js of the main file:

The Code is as follows:


Var server = require ("./server ");
Var router = require ("./router ");
Var requestHandlers = require ("./requestHandlers ");

Var handle = {};
Handle ["/"] = requestHandlers. start;
Handle ["/start"] = requestHandlers. start;
Handle ["/upload"] = requestHandlers. upload;

Server. start (router. route, handle );

For example, if I want to add a/show ing, add handle ["/show"] requestHandlers. show;

Haha, is the code more concise and orderly ?!

Next we will pass the handle object to the server, and modify server. js as follows:

The 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. 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;

Modify the route () function in the route. js file accordingly:

The Code is as follows:


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;

We pass the handle object as a parameter to the server, and then receive it by the route. Finally, the route determines whether the request handler corresponding to the current path exists. If yes, the corresponding function is called.

We can get the request processing function from the passed object in the same way as getting elements from the associated array, so there is a concise and smooth form such as handle [pathname] (); as mentioned in the previous article: "Hi, Please handle this path for me ".

In this way, we can make different processing based on different requests.

In the next section, we will further transform the code so that the server can provide some practical feedback.

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.