Nodejs根据具体请求路径执行具体操作-创新互联
1.处理请求模块(requestHandlers.js)
成都创新互联是专业的静安网站建设公司,静安接单;提供成都网站设计、网站制作,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行静安网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!function start(){
console.log("Request handler 'start' was called ");
return "Hello start";
}
function upload(){
console.log("Request handler 'upload' was called ");
return "Hello Upload";
}
exports.start = start;
exports.upload = upload;
2.路由模块(route.js)
function route(handle,pathname){
console.log("About to route a request for "+pathname);
if (typeof handle[pathname] == 'function') {
return handle[pathname]();
}else{
console.log("No request handler found for " + pathname);
return "404 Not found";
}
}
exports.route = route;
3.服务器模块(server.js)
var http = require("http");
var url = require("url");
function start(route,handle){
function onRequest(request,response){
var pathname = url.parse(request.url).pathname;
if (pathname != "/favicon.ico") {
console.log("Request for" + pathname + " received");
response.writeHead(200,{"Content-Type":"text/plain"});
var content = route(handle,pathname);
response.write(content);
response.end();
}
}
http.createServer(onRequest).listen(8888);
console.log("Server has started");
}
exports.start = start;
4.调用相应模块(index.js)
var server = require("./server");
var router = require("./route");
var requestHandlers = require("./requestHandlers");
var handle = {};
handle["/"] = requestHandlers.start;
handle["/start"] = requestHandlers.start;
handle["/upload"] = requestHandlers.upload;
server.start(router.route,handle);
5.执行index.js
node index.js
访问:http://localhost:8888/start
输出结果:
Hello start
访问:http://localhost:8888/upload
输出结果:
Hello Upload
访问:http://localhost:8888/other
输出结果:
404 Not found
另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
分享名称:Nodejs根据具体请求路径执行具体操作-创新互联
本文来源:http://hbruida.cn/article/doohis.html