由于GET请求直接被嵌入在路径中,URL是完整的请求路径,包括了?后面的部分,因此你可以手动解析后面的内容作为GET请求的参数。

node.js 中 url 模块中的 parse 函数提供了这个功能。

  1. var http = require('http');
  2. var url = require('url');
  3. var util = require('util');
  4. http.createServer(function(req, res){
  5. res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
  6. res.end(util.inspect(url.parse(req.url, true)));
  7. }).listen(3000);

  在浏览器中访问 http://127.0.0.1:8888/index?a=b&c=d然后查看返回结果:

  1. Url {
  2. protocol: null,
  3. slashes: null,
  4. auth: null,
  5. host: null,
  6. port: null,
  7. hostname: null,
  8. hash: null,
  9. search: '?a=b&c=d',
  10. query: [Object: null prototype] { a: 'b', c: 'd' },
  11. pathname: '/index',
  12. path: '/index?a=b&c=d',
  13. href: '/index?a=b&c=d'
  14. }

  我们可以使用 url.parse 方法来解析 URL 中的参数,代码如下:

  1. //
  2.  
  3. var http = require('http');
  4. var url = require('url');
  5. var util = require('util');
  6.  
  7. http.createServer(function(req, res){
  8. res.writeHead(200, {'Content-Type': 'text/plain;;charset=utf-8'});
  9.  
  10.  
  11. // 解析 url 参数
  12. var params = url.parse(req.url, true).query;
  13. res.write("网站名:" + params.name);
  14. res.write("\n");
  15. res.write("网站 URL:" + params.url);
  16. res.end();
  17.  
  18. }).listen(8888);

  在浏览器中访问 http://127.0.0.1:8888/index?name=tom&url=madou.com然后查看返回结果:

  1. 网站名:tom
  2. 网站 URLmadou.com

 

  POST 请求的内容全部的都在请求体中,http.ServerRequest 并没有一个属性内容为请求体,原因是等待请求体传输可能是一件耗时的工作。

比如上传文件,而很多时候我们可能并不需要理会请求体的内容,恶意的POST请求会大大消耗服务器的资源,所以 node.js 默认是不会解析请求体的,当你需要的时候,需要手动来做。

 

  1. var http = require('http');
  2. var querystring = require('querystring');
  3. var util = require('util');
  4.  
  5. http.createServer(function(req, res){
  6. // 定义了一个post变量,用于暂存请求体的信息
  7. var post = '';
  8.  
  9. // 通过req的data事件监听函数,每当接受到请求体的数据,就累加到post变量中
  10. req.on('data', function(chunk){
  11. post += chunk;
  12. });
  13.  
  14. // 在end事件触发后,通过querystring.parse将post解析为真正的POST请求格式,然后向客户端返回。
  15. req.on('end', function(){
  16. post = querystring.parse(post);
  17. res.end(util.inspect(post));
  18. });
  19. }).listen(3000);

  

 

 

  

 

  

  1. var url = require("url");
  2. var querystring = require("querystring");
  3.  
  4. string = "http://localhost:8888/start?foo=bar&hello=world";
  5.  
  6.  
  7. //使用url模块
  8. console.log("使用url模块。。。。")
  9. console.log("query:" + " " + url.parse(string).query)
  10. console.log("path:" + " " + url.parse(string).path)
  11. console.log("pathname:" + " " + url.parse(string).pathname)
  12.  
  13.  
  14. //使用querystring模块
  15. console.log("使用querystring模块。。。。。。")
  16. console.log(querystring.parse(string))
  17.  
  18.  
  19. //使用全局变量URL
  20. console.log("URL模块。。。。。。。。。。。。。。。。")
  21. var myUrl = new URL(string);
  22. console.log(myUrl)
  23. console.log("myUrl_pathName:" + " " + myUrl.pathname)
  24. console.log("foo:" + " " + myUrl.searchParams.get("foo"))
  25. console.log("hello:" + " " + myUrl.searchParams.get("hello"))

  

版权声明:本文为阿布alone原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/tjp40922/p/16059329.html