1 server.js
var http = require (' http ');
var url = require (' URL ');
function Start (Route,handler)
{
function ONrequest (req,res)
{
var pathName = Url.parse (req.url). PathName;
The route function that the client page passes over
Route (Pathname,handler,res);
}
Http.createserver (ONrequest). Listen (5000);
Console.log (' Server Started ');
}
Exports.start = start;
2 Route.js
Function route (pathname,handler,res)
{
Console.log (' Router user request URL: ' +pathname+ ' \ n ');
if (typeof handler[pathname]=== ' function ')
{
Handler[pathname] (res);
}else{
Console.log (' No request handler found for ' +pathname);
}
}
Exports.route = route;
3 Requesthandlers.js
var exec = require (' child_process '). exec;
Using response direct output will not block other requests
function Start (res) {
EXEC ("Ls-lah", function (Error, stdout, stderr) {
Res.writehead (200,{"Content-type": "Text/plain"});
Res.write (' Start ... ');
Res.write (stdout);
Res.write (' End ... ');
Res.end ();
});
}
function upload (res) {
Res.writehead (200,{"Content-type": "Text/plain"});
Res.write (stdout);
Res.end ();
}
Exports.start = start;
Exports.upload = upload;
4 Index.js
var server = require ('./server ');
var route = require ('./route ');
var requesthandlers = require ('./requesthandlers ');
var handle ={}
handle["/"]= Requesthandlers.start;
handle["/start"]= Requesthandlers.start;
handle["/upload"]= requesthandlers.upload;
Server.start (Route.route,handle);
Parsing starts from Index.js the handle object stores the route string and method pointers
Server.js gets the path of the current user request to execute the path corresponding to the method actually performed is the method in Requesthandlers
Lesson six self-routing improvements, responding to the paths of different requests