IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

十四、Node.js 开发 URL 路由

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

文章永久连接:https://tech.souyunku.com/?p=5444

Node.js 路由(router) 提供了 URL 请求路径到 Node.js 方法的一一映射机制

我们可以解析 HTTP 请求的 URL ,从 URL 中提取出请求的路径以及 GET/POST 参数

Node.js Web 应用程序所有的请求数据都被封装在 request 对象中,该对象作为 onRequest() 回调函数的第一个参数传递

我们可以使用 Node.js url 模块和 querystring 第三方模块来解析这些请求参数 :

1、 Node.js url 模块可以解析 URL 参数信息
2、 querystring 可以解析 POST 请求中放在 body(请求体中的参数)

URL 解析

解析 URL Query 参数图示

url.parse(string).query
                                           |
           url.parse(string).pathname      |
                       |                   |
                       |                   |
                     ------ -------------------
http://localhost:8080/ss?name=souyunku&hello=world
                              ----       -----
                                |          |
                                |          |
querystring.parse(queryString)["name"]     |
                                           |
          querystring.parse(queryString)["hello"]

范例

我们可以给 http.createServer 的 onRequest 回调添加一些逻辑,用来找出浏览器请求 URL 的 PATH 路径

main.js

/*
 * filename: main.js
 * author: 搜云库技术团队(tech.souyunku.com)
 * Copyright © 2015-2065 tech.souyunku.com. All rights reserved.
*/

var http = require("http");
var url = require("url");

function serv() {
  function onRequest(req, res)
  {
    var pathname = url.parse(req.url).pathname;
    res.write("<h1>Hello World</h1>");
    res.write("<p>请求的路径是:" + pathname + "</p>")
    res.end();
  }

  http.createServer(onRequest).listen(8080);
  console.log("Server has started.");
}

//exports.serv = serv;
serv();

运行以上 Node.js 范例,输出结果如下

$ node main.js
Server has started.

在浏览器上打开 http://localhost:8080/ss?name=souyunku&hello=world 显示如下

img_1.png

现在我们的应用可以根据 URL 路径来区别不同请求了

这使我们可以使用路由(还未完成)来将请求以 URL 路径为基准映射到处理程序上

也就是说在我们所要构建的应用中,这意味着来自 /start 和 /upload 的请求可以使用不同的代码来处理

自制路由器

有了上面的基础,我们就可以自己开发一个简单的路由器了

新建一个文件 router.js 添加以下内容

router.js

function route(pathname) {
  console.log("About to route a request for " + pathname);
}

exports.route = route;

这段代码现在什么也没做,这是因为我们还没添加更多的逻辑

我们先来看看如何把路由和服务器整合起来

我们的服务器应当知道路由的存在并加以有效利用,我们当然可以通过硬编码的方式将这一依赖项绑定到服务器上,但是其它语言的编程经验告诉我们这会是一件非常痛苦的事,因此我们将使用依赖注入的方式较松散地添加路由模块

我们先扩展下服务器的 serv() 函数,以便将路由函数作为参数传递过去

加载路由模块

var router = require("./router");

修改 serv() 函数

main.js

/*
 * filename: main.js
 * author: 搜云库技术团队(tech.souyunku.com)
 * Copyright © 2015-2065 tech.souyunku.com. All rights reserved.
*/

var http = require("http");
var url = require("url");
var router = require("./router");

function serv( route ) {
  function onRequest(req, res)
  {
    var pathname = url.parse(req.url).pathname;

    route(pathname);

    res.write("<h1>Hello World</h1>");
    res.write("<p>请求的路径是:" + pathname + "</p>")
    res.end();
  }

  http.createServer(onRequest).listen(8080);
  console.log("Server has started.");
}

//exports.serv = serv;
serv();

当然了,看起来我们仍旧什么都没做

运行以上 Node.js 脚本,输出结果如下

$ node main.js
Server has started.

然后用浏览器打开 http://localhost:8080/ss?name=souyunku&hello=world 显示如下

img_2.png

说明我们开发的路由器已经起作用了

干货推荐

本站推荐:精选优质专栏

附录:Node.js 教程:系列文章


Warning: A non-numeric value encountered in /data/wangzhan/tech.souyunku.com.wp/wp-content/themes/dux/functions-theme.php on line 1154
赞(78) 打赏



未经允许不得转载:搜云库技术团队 » 十四、Node.js 开发 URL 路由

IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码
IDEA2023.1.3破解,IDEA破解,IDEA 2023.1破解,最新IDEA激活码

评论 抢沙发

大前端WP主题 更专业 更方便

联系我们联系我们

觉得文章有用就打赏一下文章作者

微信扫一扫打赏

微信扫一扫打赏


Fatal error: Uncaught Exception: Cache directory not writable. Comet Cache needs this directory please: `/data/wangzhan/tech.souyunku.com.wp/wp-content/cache/comet-cache/cache/https/tech-souyunku-com/index.q`. Set permissions to `755` or higher; `777` might be needed in some cases. in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php:367 Stack trace: #0 [internal function]: WebSharks\CometCache\Classes\AdvancedCache->outputBufferCallbackHandler() #1 /data/wangzhan/tech.souyunku.com.wp/wp-includes/functions.php(5109): ob_end_flush() #2 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(303): wp_ob_end_flush_all() #3 /data/wangzhan/tech.souyunku.com.wp/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters() #4 /data/wangzhan/tech.souyunku.com.wp/wp-includes/plugin.php(470): WP_Hook->do_action() #5 /data/wangzhan/tech.souyunku.com.wp/wp-includes/load.php(1097): do_action() #6 [internal function]: shutdown_action_hook() #7 {main} thrown in /data/wangzhan/tech.souyunku.com.wp/wp-content/plugins/comet-cache/src/includes/traits/Ac/ObUtils.php on line 367