[node] Node.js的路由

[node] Node.js的路由

  • 路由 & 路由解析
  • 路由信息的整合
    • URL信息
    • 路由处理逻辑
    • 路由逻辑与URL信息的整合
    • 路由的使用

路由 & 路由解析

路由需要提供请求的 URL 和其他需要的 GET/POST 参数,随后路由需要根据这些数据来执行相应的代码。

因此,根据 HTTP 请求,从中提取出请求的 URL 以及 GET/POST 参数。这一功能应当属于路由还是服务器(亦或一个单独模块的功能)确实值得探讨,但这里暂定其为HTTP服务器的功能。

目前所有数据都会包含在 request 对象中,该对象作为 onRequest() 回调函数的第一个参数传递。但是为了解析这些数据,我们需要额外的 Node.JS 模块,它们分别是 url 和 querystring 模块。

ts 复制代码
var url = require("url");
var querystring = require("querystring");

var data = "http://localhost:8888/start?foo=bar&hello=world";

const query = url.parse(data).query;
console.log(query); // foo=bar&hello=world
console.log(url.parse(data).pathname); // /start
console.log(querystring.parse(query)["foo"]); // bar
console.log(querystring.parse(query)["hello"]); // world

路由信息的整合

URL信息

server.js ,用于获取浏览器URL地址,该信息是路由需要使用到的信息:

ts 复制代码
var http = require("http");
var url = require("url");
 
function start() {
  function onRequest(request, response) {
    var pathname = url.parse(request.url).pathname;
    console.log("Request for " + pathname + " received.");
    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World");
    response.end();
  }
 
  http.createServer(onRequest).listen(8888);
  console.log("Server has started.");
}
 
exports.start = start;

以上代码达成通过 URL 路径来区别不同请求--使得路由之后(还未完成)可以将请求(以 URL 路径为基准)映射到处理程序上。

路由处理逻辑

router.js 简单的路由处理逻辑模块:

ts 复制代码
function route(pathname) {
  console.log("About to route a request for " + pathname);
}
exports.route = route;

以上代码没有具体的逻辑处理,你之后可以根据需要添加,此时只是封装了路由的处理方法。

路由逻辑与URL信息的整合

然后,先来看看如何把路由和服务器整合起来。

服务器知道路由的存在并应加以有效利用。可以通过硬编码的方式将这一逻辑处理绑定到服务器上,但是对于推荐模块开发的情况下,使用依赖注入的方式较松散地添加路由模块会使得耦合性更低,也方便以后的代码变更。

所以对于server.js 文件做出如下更改,将路由函数作为参数传递过去:

ts 复制代码
var http = require("http");
var url = require("url");
 
function start() {
  function onRequest(request, response) {
    var pathname = url.parse(request.url).pathname;
    console.log("Request for " + pathname + " received.");

    route(pathname);

    response.writeHead(200, {"Content-Type": "text/plain"});
    response.write("Hello World");
    response.end();
  }
 
  http.createServer(onRequest).listen(8888);
  console.log("Server has started.");
}
 
exports.start = start;

路由的使用

index.js 文件,服务器使用路由的方式--路由函数注入到服务器中:

ts 复制代码
var server = require("./server");
var router = require("./router");
 
server.start(router.route);

以上整体是对路由信息的获取与如何添加路由处理逻辑的一个简单介绍,是一种方法上的介绍

相关推荐
笑醉踏歌行1 小时前
NVM,Node.Js 管理工具
运维·ubuntu·node.js
chxii3 小时前
1.4 Node.js 的 TCP 和 UDP
node.js
xd0000216 小时前
11. vue pinia 和react redux、jotai对比
node.js
程序猿小D17 小时前
第16节 Node.js 文件系统
linux·服务器·前端·node.js·编辑器·vim
前端老六喔1 天前
🎉 开源项目推荐 | 让你的 TypeScript/React 项目瘦身更简单!
node.js·前端工程化
醉书生ꦿ℘゜এ1 天前
npm error Cannot read properties of null (reading ‘matches‘)
前端·npm·node.js
超级土豆粉1 天前
从0到1写一个适用于Node.js的User Agent生成库
linux·ubuntu·node.js
空中湖1 天前
‘pnpm‘ 不是内部或外部命令,也不是可运行的程序
npm·node.js
SailingCoder1 天前
grafana-mcp-analyzer:基于 MCP 的轻量 AI 分析监控图表的运维神器!
运维·人工智能·typescript·node.js·grafana
又又呢1 天前
前端面试题总结——webpack篇
前端·webpack·node.js