您现在的位置是:主页 > news > 广州市研发网站建设怎么样/百度网站客服

广州市研发网站建设怎么样/百度网站客服

admin2025/6/8 12:44:53news

简介广州市研发网站建设怎么样,百度网站客服,深圳网站建设定制开发,哈尔滨建站系统Node.js 路由 路由是干什么的? 路由是通过设置路径访问相应的函数。 为路由提供请求的URL和其他需要的GET及POST参数,随后路由需要根据这些数据来执行相应的代码。 需要的数据从http请求中来,包含在request对象中,为了解析这些数…

广州市研发网站建设怎么样,百度网站客服,深圳网站建设定制开发,哈尔滨建站系统Node.js 路由 路由是干什么的? 路由是通过设置路径访问相应的函数。 为路由提供请求的URL和其他需要的GET及POST参数,随后路由需要根据这些数据来执行相应的代码。 需要的数据从http请求中来,包含在request对象中,为了解析这些数…

Node.js 路由

路由是干什么的?
路由是通过设置路径访问相应的函数。
为路由提供请求的URL和其他需要的GET及POST参数,随后路由需要根据这些数据来执行相应的代码。
需要的数据从http请求中来,包含在request对象中,为了解析这些数据,需要额外的Node.JS模块,它们分别是url和querystring模块。

url.parse(string).query|url.parse(string).pathname      ||                   ||                   |------ -------------------
http://localhost:8888/listCategory---       -----|          ||          |querystring(string)["foo"]    ||querystring(string)["hello"]

对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;      //获取路径var html = route(handle, pathname); response.writeHead(200, {"Content-Type": "text/plain"}); response.write(html); response.end(); } http.createServer(onRequest).listen(8888); console.log("Server has started.");
} exports.start = start; 

路由router.js

function route(handle, pathname) { if (typeof handle[pathname] === 'function') { return handle[pathname](); } else {return pathname + ' is not defined';} 
} 
exports.route = route; 

业务处理模块 requestHandlers.js

提供两个函数,返回不同的字符串。

//业务处理模块
function listCategory() { return "a lot of categorys";
} function listProduct() { return "a lot of products";
} exports.listCategory = listCategory; 
exports.listProduct = listProduct; 

入口主模块 index.js

与以往启动使用 server.js不同,带路由功能,一般都会通过 index.js 启动,所以index.js 就是入口模块。
handle 是一个数组,映射了不同的访问路径与 业务处理模块对应函数的一一对应关系。

handle["/listCategory"] = requestHandlers.listCategory;  

这就表示访问路径 /listCategory的话,就会交给函数 requestHandlers.listCategory 来处理。

var server = require("./server"); 
var router = require("./router"); 
var requestHandlers = require("./requestHandlers"); var handle = {} 
handle["/listCategory"] = requestHandlers.listCategory; 
handle["/listProduct"] = requestHandlers.listProduct; server.start(router.route, handle); 

启动访问

通过访问入口主模块:

node index.js

在这里插入图片描述
访问:
在这里插入图片描述
在这里插入图片描述

过程

1、启动index.js
2、index.js 调用了 server.start 函数,并且传递了 router.js 里route 函数和handle数组作为参数
3. serverl.js 通过了8888端口启动了服务。 然后用 onRequest 函数来处理业务
3.1 在 onRequest 中,首先获取 访问路径 pathname
3.2 然后调用 router.js 的route 函数,并把pathname 和 handle数组传递进去
4. 在router.js 中,通过pathname为下标获调用真正的业务函数,并把业务函数的返回值返回出去。
4.1 如果找不到,比如访问 /listUser 这个路径就没有在 handle 数组中找到对应,那么就会返回 listUser is not defined.
5. 当访问地址是 /listCategory的时候, 真正的业务函数 requestHandlers.js 中的 listCategory() 就会被调用,并返回业务 Html 代码 : “a lots of categorys”。
比最初的访问要复杂一些,但是便于拓展功能模块。如果开发了新的模块,只需要在index.js中添加相应映射就行了。


参考:

【1】、http://how2j.cn/k/nodejs/nodejs-router/1763.html
【2】、https://www.runoob.com/nodejs/nodejs-router.html
【3】、https://www.w3cschool.cn/nodejs/nodejs-router.html