Easily create nodejs server (5) : event handler

  • 2020-05-07 19:11:05
  • OfStack

In order to give different feedback, we introduce an event handler module.

The module is named requestHandlers, so let's first add two placeholders, start() and upload().

requestHandlers. js code is as follows:

function start() {
    console.log(" access /star Call this when. ");
function upload() {
    console.log(" access /upload Call this when. ");
exports.start = start;
exports.upload = upload;

In a real-world application, the number of request handlers would continue to increase, and we certainly don't want to have to complete the request in routing every time we have a new URL or request handler

Mapping to the handler is a hassle.

On top of that, we don't want to have a bunch of if request == x then call handler y in the routing, which makes the code look messy and unprofessional.

Here I'm going to use the concept of associative arrays to handle this requirement, and we're going to pass the 1 series of request handlers through an object that needs to be injected into the route() function in a loosely-coupled manner.

We first introduce this object into the main file index.js:

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 mapping, just add a sentence handle["/show"] requestHandlers.show; That's it;

Haha, so 1 to the code is not much more concise and orderly? !

Next, we will send handle object to the server. server.js is modified 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");
 console.log("Server has started.");
exports.start = start;

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

function route(handle, pathname) {
 console.log("About to route a request for " + pathname);
 if (typeof handle[pathname] === 'function') {
 } else {
  console.log("No request handler found for " + pathname);
exports.route = route;

We pass the handle object to the server as a parameter, which is then received by the route. Finally, the path is used to determine whether the request handler corresponding to the current path exists or not. If it exists, the corresponding function will be called.

We can get the request handler from the passed object in the same way that we get the element 1 from the associative array, so we have a simple and smooth form like handle[pathname](); ", which feels like the following: "hey, please help me with this path".

This way, we can do different things for different requests.

In the next section, we'll take a step forward and change the code to let the server do some actual feedback.

Related articles: