【router.js】
// 加载所需模块 var http = require('http'); var url = require('url'); var fs = require('fs'); var host = '127.0.0.1'; var port = 8080; http.createServer(function(req,res){ var pathname = url.parse(req.url).pathname; console.log('Request for ' + pathname + ' received.'); function showPaper(path,status){ var cOntent= fs.readFileSync(path); res.writeHead(status, { 'Content-Type': 'text/html;charset=utf-8' }); res.write(content); res.end(); } switch(pathname){ //'首页' case '/': case '/home': showPaper('./view/home.html',200); break; //'about页' case '/about': showPaper('./view/about.html',200); break; //'404页' default: showPaper('./view/404.html',404); break; } }).listen(port, host);
【404.html】
404
【about.html】
about
【home.html】
home
更多编程相关知识,请访问:编程视频!!
以上就是使用nodejs实现路由功能的详细内容,更多请关注其它相关文章!