In the previous section, we implemented simple routing. In this section, we implemented a more complex routing process.
unified management
We initialize the routing control in server.js and start the script module.
var server = require('./start'); var router = require('./router'); var requestHandlers = require('./handlers'); var handler = {}; handler["/"] = requestHandlers.home; handler["/show"] = requestHandlers.show; handler["/upload"] = requestHandlers.upload; server.start(router,handler); console.log("Run on port 1337");
Startup script
At the end of server.js, we call the start module, which is the start. JS file. Processing in the start.js file, the code is as follows
var http = require('http'); var url = require('url'); function start(router,handler) { console.log("Starting~~"); function onRequest(req, res) { var pathname = url.parse(req.url).pathname; //Parsing url var content = router(pathname,handler); //Pass it to the router module for processing and return the processing content if (content){ res.writeHead(200,{ "Content-Type":"text/plain" }); res.write(content); res.end(); }else { res.writeHead(404,{ "Content-Type":"text/plain" }); res.write("404 not found"); res.end(); } res.writeHead(200,{ "Content-Type":"text/plain" }); res.write("The First Server"); res.end(); } var port = process.env.port || 1337; http.createServer(onRequest).listen(port); console.log("Started!!"); } exports.start = start;
The core code here is to call router(pathname,handler); this method is implemented in the router module.
Call routing corresponding methods
The router module determines whether the path composed of the parameter pathname and handler is implemented. If so, call the corresponding routing method, router.js code is as follows
function route(pathname,handler) { console.log("Route for path requested:" + pathname); if (typeof hanlder[pathname] == 'function'){ //Determine whether a routing exists handler[pathname](); //Existing methods call corresponding routing methods }else { console.log("No Method found for " + pathname); return null; } } exports.route = route;
Multiple routing methods
First of all, we need to define multiple routing methods. In order to facilitate unified management and control, we write the routing methods in a handlers.js file. First, we define three methods. Here we just realize the simplest printing.
function home() { console.log("Request 'home' called."); } function show() { console.log("Request 'show' called."); } function upload() { console.log("Request 'upload' called."); } exports.home = home; exports.show = show; exports.upload = upload;
Last
In the project:
1.server.js predefines the processing routing method, and then calls the method to start the server (start.js).
2.start.js is responsible for starting the service, parsing the path passed by the url, and passing the URL to router.js.
3.router.js to determine whether the routing is correct, and then call the corresponding url processing method, the processing method is implemented in handlers.js.
4.handlers.js performs real routing.
In this way, we manually implemented an example of nodejs routing control.