尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

HoRain云--Node.js 路由

HoRain云--Node.js 路由 在 Node.js 中路由是处理 HTTP 请求的关键部分它决定了如何根据不同的 URL 和 HTTP 方法如 GET、POST、PUT、DELETE 等来分发请求。路由通常用于构建 Web 应用程序特别是 RESTful API。Node.js 本身并没有内置的路由机制但可以通过中间件库如 Express来实现。路由通常涉及以下几个方面URL 匹配根据请求的 URL 来匹配路由规则。HTTP 方法匹配根据请求的 HTTP 方法GET、POST、PUT、DELETE 等来匹配路由规则。请求处理一旦匹配到合适的路由规则就调用相应的处理函数来处理请求。Node.js 中我们可以通过 http 模块创建一个简单的路由如下实例实例const http require(http);// 创建服务器并定义路由const server http.createServer((req, res) {const { url, method } req;if (url / method GET) {res.writeHead(200, { Content-Type: text/plain });res.end(Home Page);} else if (url /about method GET) {res.writeHead(200, { Content-Type: text/plain });res.end(About Page);} else {res.writeHead(404, { Content-Type: text/plain });res.end(404 Not Found);}});server.listen(3000, () {console.log(Server is running on http://localhost:3000);});浏览器访问http://localhost:3000显示如下浏览器访问http://localhost:3000/about显示如下如果访问其他 URL 地址则会直接显示404 Not Found。更多 HTTP 请求可以参考HTTP 教程。请求参数一个完整 URL 的 http://localhost:8888/start?foobarhelloworld 包含主机、路径和查询字符串。为了解析这些数据我们可以使用 URL 对象和 querystring 模块。const myUrl new URL(http://localhost:8888/start?foobarhelloworld); // 提取路径名 console.log(myUrl.pathname); // 输出: /start // 提取查询参数 console.log(myUrl.searchParams.get(foo)); // 输出: bar console.log(myUrl.searchParams.get(hello)); // 输出: worldmyUrl.pathname | | ----- http://localhost:8888/start?foobarhelloworld --- ----- | | | | myUrl.searchParams.get(foo) | | myUrl.searchParams.get(hello)当然我们也可以用 querystring 模块来解析 POST 请求体中的参数相关内容后面的 Node.js GET/POST请求 会介绍。我们需要的所有请求数据都会包含在 request 对象中该对象作为 onRequest() 回调函数的第一个参数传递。现在我们来给 http 模块的 onRequest() 函数加上一些逻辑用来找出浏览器请求的 URL 路径server.js 文件代码var http require(http); var url require(url); function start() { function onRequest(request, response) { // 使用 URL 构造函数解析请求路径 const pathname new URL(request.url, http://${request.headers.host}).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.); // 打印服务器启动消息 } // 导出 start 函数供其他模块使用 module.exports.start start;好了我们的应用现在可以通过请求的 URL 路径来区别不同请求了--这使我们得以使用路由还未完成来将请求以 URL 路径为基准映射到处理程序上。在我们所要构建的应用中这意味着来自 /start 和 /upload 的请求可以使用不同的代码来处理。稍后我们将看到这些内容是如何整合到一起的。现在我们可以来编写路由了建立一个名为router.js的文件添加以下内容router.js 文件代码function route(pathname) { console.log(About to route a request for pathname); } // 导出了 route 函数 exports.route route;router.js 处理路由逻辑定义并导出 route 函数用于在服务器收到请求时处理不同路径。我们的服务器应当知道路由的存在并加以有效利用。我们当然可以通过硬编码的方式将这一依赖项绑定到服务器上但是其它语言的编程经验告诉我们这会是一件非常痛苦的事因此我们将使用依赖注入的方式较松散地添加路由模块。首先我们来扩展一下服务器的 start() 函数以便将路由函数作为参数传递过去server.js文件代码如下server.js 文件代码// server.js const http require(http); // 引入 Node.js 的 http 模块用于创建服务器 const { URL } require(url); // 从 url 模块引入 URL 构造函数 // 定义并导出 start 函数用于启动服务器 function start(route) { // 定义 onRequest 函数处理每个请求 function onRequest(request, response) { // 使用 URL 构造函数解析请求路径 const pathname new URL(request.url, http://${request.headers.host}).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.); } // 导出 start 函数供其他模块使用 module.exports.start start;server.js 定义了服务器的启动逻辑并在接收到请求时调用路由函数。同时我们会相应扩展 index.js使得路由函数可以被注入到服务器中index.js 文件代码var server require(./server); var router require(./router); server.start(router.route);index.js 是程序的入口文件负责启动服务器并将路由模块传入服务器模块中。现在启动应用node index.js始终记得这个命令行随后请求一个URL你将会看到应用输出相应的信息这表明我们的 HTTP 服务器已经在使用路由模块了并会将请求的路径传递给路由$ node index.js Server has started.在浏览器中访问http://localhost:8888/服务器应会在控制台打印路径相关的路由消息并返回Hello World响应后台终端会显示访问信息Request for / received. About to route a request for / Request for /favicon.ico received. About to route a request for /favicon.ico使用 Express 进行路由Express 是一个流行的 Node.js 框架它提供了强大的路由功能。更多关于 Express 内容可以参考Node.js Express 框架。安装 Express首先确保你已经安装了 Express如果还没有安装可以使用 npm 来安装npm install express基本路由以下是一个简单的 Express 应用程序展示了如何设置基本的路由实例const express require(express);const app express();const port 3000;// 定义一个 GET 路由app.get(/, (req, res) {res.send(Hello, World!);});// 定义一个 POST 路由app.post(/submit, (req, res) {res.send(Form submitted!);});// 启动服务器app.listen(port, () {console.log(Server is running on http://localhost:${port});});动态路由动态路由允许你使用参数化的 URL。例如你可以定义一个路由来处理 /users/:id其中 :id 是一个动态参数。app.get(/users/:id, (req, res) { const userId req.params.id; res.send(User ID: ${userId}); });路由参数Express 允许你从 URL 中提取参数并通过 req.params 对象访问这些参数。实例app.get(/users/:id, (req, res) {const userId req.params.id;res.send(User ID: ${userId});});app.get(/search/:query, (req, res) {const query req.params.query;res.send(Search query: ${query});});查询参数查询参数是 URL 中的键值对通常用于传递额外的信息。你可以通过 req.query 对象访问查询参数。app.get(/search, (req, res) { const query req.query.q; res.send(Search query: ${query}); });路由中间件路由中间件是在处理请求之前或之后执行的函数。你可以使用中间件来处理诸如身份验证、日志记录等任务。// 日志记录中间件 const logger (req, res, next) { console.log(Request Type: ${req.method} ${req.url}); next(); }; // 使用中间件 app.use(logger); app.get(/, (req, res) { res.send(Hello, World!); });路由分组为了更好地组织代码你可以使用路由分组。Express 提供了 express.Router 对象可以用来创建模块化的、可挂载的路由处理程序。实例const express require(express);const app express();const port 3000;// 创建一个路由器实例const userRouter express.Router();// 定义用户相关的路由userRouter.get(/, (req, res) {res.send(List of users);});userRouter.get(/:id, (req, res) {const userId req.params.id;res.send(User ID: ${userId});});// 挂载用户路由器app.use(/users, userRouter);// 启动服务器app.listen(port, () {console.log(Server is running on http://localhost:${port});});高级路由技巧1、错误处理你可以定义错误处理中间件来捕获和处理路由中的错误。app.use((err, req, res, next) { console.error(err.stack); res.status(500).send(Something broke!); });2、路由优先级路由的定义顺序决定了它们的优先级先定义的路由会先被匹配。3、路由限制你可以使用中间件来限制某些路由的访问例如仅允许认证用户访问。实例const authMiddleware (req, res, next) {if (req.headers.authorization) {next();} else {res.status(401).send(Unauthorized);}};app.get(/admin, authMiddleware, (req, res) {res.send(Admin page);});
返回列表