nodejs路由可通过url模块实现。url模块用于处理客户端请求过来的URL,nodejs中可通过url模块对url地址进行解析,从而实现路由的操作。

本教程操作环境:windows7系统、nodejs 12.19.0版,DELL G3电脑。
nodejs 的路由
路由指的就是我们要针对不同的URL有不同的处理方式。
URL模块作用:处理客户端请求过来的URL
nodejs可通过 url 模块对 url 地址进行解析, 实现路由的操作
const http = requrie('http')
const url = requrie('url')
http.createServer((req, res) => {
// 对请求的路径进行解析
let pathname = url.parse(req.url).pathname;
if(pathname == '/'){
// 相关的处理
}else if(pathname == '/login'){
// 相关的处理
}....
}).listen(3000, () => {
console.log('server runnning ....')
})更多node相关知识,请访问:node教程!!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号