您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
三六零分类信息网 > 绥化分类信息网,免费分类信息发布

nodeJS实现路由功能实例详解

2026/1/13 16:08:12发布44次查看
前面的话本文将使用nodejs实现较复杂应用的路由功能
结构项目结构如下
代码如下
功能【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】
<!doctype html> <html lang="en"> <head>     <meta charset="utf-8">     <title>document</title> </head> <body> 404     </body> </html>
【about.html】
<!doctype html> <html lang="en"> <head>     <meta charset="utf-8">     <title>document</title> </head> <body>about     </body> </html>
【home.html】
<!doctype html> <html lang="en"> <head>     <meta charset="utf-8">     <title>document</title> </head> <body>home     </body> </html>
演示
以上就是nodejs实现路由功能实例详解的详细内容。
绥化分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product