Routing is that different URLs have different processing methods, such as/start's "business logic" and/upload differences.
In today's reality, the routing process "ends" in the routing module, and the routing module is not the handler module that really acts on the request, so when the handler changes, the content that needs to be modified does not involve routing.
Routing is usually set when the request handler is ready.
When an application needs a new part, it needs to add a new module. You can create a requesthandlers module, and for each request handler, add a placeholder function, which is then exported as a module.
Here is an example:
------Requesthandlers.js
------Router.js
------Server.js
------Index.js
Requesthandlers.js
/*requesthandlers.js module, add a placeholder function for each request handler, and finally export these methods as modules */function start () {Console.log ("request handler ' start ' was Called. "); function upload () {console.log ("Request handler ' upload ' was called."); Exports.start = Start;exports.upload = upload;
Router.js
The/*router module *//* checks if the request handler for the given path in the URL exists, if there is a direct call to the corresponding function */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;
Server.js
/*server module *//* Request processing module, take the path from the URL, and leave it to router.js to check that the path has no corresponding processing function */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);//Here you can do different processing of different paths//if (pathname== "...") Response.writehead different information such as if (pathname== "/") {Response.writehead (200,{"Content-type": "Text/plain"}); Response.Write ("Pathname:/"); Response.End ();} if (pathname== "/start") {Response.writehead (200,{"Content-type": "Text/plain"}); Response.Write ("Pathname:/start") ; Response.End ();} if (pathname== "/upload") {Response.writehead (200,{"Content-type": "Text/plain"}), Response.Write ("Pathname:/upload "); Response.End ();}} Http.createserver (onrequest). Listen, Console.log ("Server has Started");}
Index.js
/*index module *//* boot module, main module, handle collection object inside, is a collection of array elements pathname and processing functions, this collection is router.js inside to check the basis */var Server = require (". 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);
Console launch app:
Node Index.js
Enter the following URL request in the browser:
http://127.0.0.1:3000
Console display:
Browser display:
Enter the following URL request in the browser:
Http://127.0.0.1:3000/start
Console display:
Browser display:
Enter the following URL request in the browser:
Http://127.0.0.1:3000/upload
Console display:
Browser display:
Enter the following URL request in the browser:
Http://127.0.0.1:3000/start1
Console display:
node. js-------route after adding handler functions