In order to provide different feedback on different requests, we introduce an event processor module.
The module is named requestHandlers. We first add two placeholder functions, start() and upload().
The code for requestHandlers.js is as follows:
The code copy is as follows:
function start() {
console.log("Call this when accessing /star.");
}
function upload() {
console.log("Call this when accessing/upload.");
}
exports.start = start;
exports.upload = upload;
In real applications, the number of request handlers will continue to increase. Of course, we do not want to complete the request in the route every time there is a new URL or request handler.
Mapping to the handler repeatedly.
In addition, we don't 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 associative arrays to handle this requirement. We pass a series of request handlers through an object and 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:
The code copy 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 map, just add a handle["/show"] requestHandlers.show;
Haha, is the code much more concise and orderly? !
Next, we pass the handle object to the server, and the server.js is modified as follows:
The code copy 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 correspondingly:
The code copy 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, then receive it by the route, and finally the route determines whether the request handler corresponding to the current path exists or not. If it exists, call the corresponding function.
We can get the request handling function from the passed object in the same way as getting elements from the associative array, so we have a simple and smooth expression like handle[pathname]();, which feels like the one mentioned earlier: "Hi, 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 revise the code to allow the server to make some actual feedback operations.