Easy to create Nodejs server (5): event handler _node.js

Source: Internet
Author: User

To make a different response to the difference, we introduce an event handler module.

The module is named Requesthandlers, and we add start () and upload () two placeholder functions.

The Requesthandlers.js code is as follows:

Copy Code code as follows:

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

function upload () {
Console.log (this is called when accessing/upload.) ");
}

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

In real applications, the number of request handlers will increase, and we certainly do not want to have a new URL or request handler every time, in order to complete the request in the route

To the mapping of the handler and toss it back and forth.

In addition, we don't want to have a lot of If Request = X then call handler y in the route, which makes the code look messy and unprofessional.

Here I will use the concept of associative arrays to handle this requirement, we pass a series of request handlers through an object, and we need to inject this object into the route () function in a loosely coupled way.

Let's first introduce this object into the main file index.js:

Copy Code code 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, I would like to add a/show mapping, directly adding a sentence handle["/show"] requesthandlers.show;

Haha, so the code is not simple and more orderly?!

Next we pass the handle object to the server, server.js the following modifications:

Copy Code code 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 ({"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:

Copy Code code 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 will handle object as parameters to the server, and then by the route received, and finally by routing to determine the current path of the corresponding request handler exists, the existence of the call to the corresponding function.

We can get the request handler function from the passed object in the same way that we get the element from the associative array. So there is a concise and fluent form like handle[pathname] (), the expression, the feeling is as mentioned in the front: "Hey, please help me deal with this path."

In this way, we can make different processing according to different requests.

In the next section, we will further transform the code to allow the server to make some actual feedback operations.

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.