一、server+router
1、构成基本框架;
index.js(运行的主页面)+server.js(服务器页面)+router.js(路由页面)
2、页面详解
2.1、index.js页面
①分别调用并启用server.js+router.js页面;
②代码详细;
var server=require("server.js");
var router=require("router.js");
//将路由模块以参数形式传递给服务器
server.start(router.route);
2.2、server.js页面
①创建服务器并应用相应模块;
②代码详细:
var http=require("http");
var url=require("url");
function start(route){
http.createServer(function(req,res){
var pathname=url.parse(req.url).pathname;
console.log("request for "+pathname+" reserved!")
route(pathname);
res.writeHead(200,{"Content-Type":"text/plain"});
res.write("hello world!");
res.end();
}).listen(3000)
console.log("server running!")
}
exports.start=start;
2.3、router.js页面
①创建路由模块;
②代码:
function route(pathname){
console.log("成功!")
}
exports.route=route;
二、server+router+handler
1、构成基本框架;
index.js(运行的主页面)+server.js(服务器页面)+router.js(路由页面)+requesthandler.js(请求处理程序页面)
2、页面详解
2.1、index.js页面
①分别调用并启用server.js+router.js+requesthandler.js页面;
②代码详细;
var server=require("server.js");
var router=require("router.js");
var requestHandler=require("requesthandler.js")
//定义请求处理对象
var handler={};
handler["/"]=requestHandler.start;
handler["/start"]=requestHandler.start;
handler["/upload"]=requestHandler.upload;
//将路由模块及请求处理对象以参数形式传递给服务器
server.start(router.route,handler);
2.2、server.js页面
①创建服务器并应用相应模块;
②代码详细:
var http=require("http");
var url=require("url");
function start(route,handler){
http.createServer(function(req,res){
var pathname=url.parse(req.url).pathname;
console.log("request for "+pathname+" reserved!")
route(handler,pathname);
res.writeHead(200,{"Content-Type":"text/plain"});
res.write("hello world!");
res.end();
}).listen(3000)
console.log("server running!")
}
exports.start=start;
2.3、router.js页面
①创建路由模块;
②代码:
function route(handler,pathname){
if(typeof(handler[pathname]==="function")){
handler[pathname]()
}else{
console.log("No request handler found for " + pathname);
}
}
exports.route=route;
2.4、requestHandler.js页面
①创建请求处理程序
②代码:
function start(){
console.log("Request handler 'start' was called.")
}
function upload(){
console.log("Request handler 'upload' was called.");
}
exports.start=start;
exports.upload=upload;
三、server+router+handler(非阻塞运行)△
1、构成基本框架;
index.js(运行的主页面)+server.js(服务器页面)+router.js(路由页面)+requesthandler.js(请求处理程序页面)
2、页面详解
2.1、index.js页面
①分别调用并启用server.js+router.js+requesthandler.js页面;
②代码详细;
var server=require("server.js");
var router=require("router.js");
var requestHandler=require("requesthandler.js")
//定义请求处理对象
var handler={};
handler["/"]=requestHandler.start;
handler["/start"]=requestHandler.start;
handler["/upload"]=requestHandler.upload;
//将路由模块及请求处理对象以参数形式传递给服务器
server.start(router.route,handler);
2.2、server.js页面
①创建服务器并应用相应模块;
②代码详细:
var http=require("http");
var url=require("url");
function start(route,handler){
http.createServer(function(req,res){
var pathname=url.parse(req.url).pathname;
console.log("request for "+pathname+" reserved!")
//将response作为参数传递到router中
route(handler,pathname,response);
//移出response的相关函数,在router及requestHandler中执行向浏览器直接输出
}).listen(3000)
console.log("server running!")
}
exports.start=start;
2.3、router.js页面
①创建路由模块;
②代码:
function route(handler,pathname,response){
if(typeof(handler[pathname]==="function")){
handler[pathname](response)
}else{
console.log("No request handler found for " + pathname);
response.writeHeader(404,{"Content-Type":"text/plain"});
response.write("404 NOT FOUND")
response.end()
}
}
exports.route=route;
2.4、requestHandler.js页面
①创建请求处理程序
②代码:
var exec=require("child-process").exec;
function start(response){
exec("ls lah",function(err,stdout,stderr){
response.writeHeader(200,{"Content-type":"text/plain"});
response.write(stdout);
response.end()
})
}
function upload(response){
response.writeHeader(200,{"content-type":"text/plain"});
response.write("hello upload!")
response.end();
}
exports.start=start;
exports.upload=upload;