目录
文章目录隐藏
  1. 获取 GET 请求内容
  2. 获取 URL 的参数

在很多场景中,我们的服务器都需要跟用户的浏览器打交道,如表单提交。

表单提交到服务器一般都使用 GET/POST 请求。

本文码云笔记将为大家介绍 Node.js GET/POST 请求。

获取 GET 请求内容

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

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

实例

新建一个 server.js 文件,代码如下:

const http = require('http');
const url = require('url');
const util = require('util');

http.createServer(function (req, res){
	res.writeHead(200, {'Content-Type':'text/plain; charset=utf-8'});
	res.end(util.inspect(url.parse(req.url, true)));
}).listen(8888);

使用 node 命令执行以上的代码:

node server.js

在浏览器中访问 http://localhost:8888/user?name=码云笔记&url=www.mybj123.com 然后查看返回结果:

在浏览器中访问查看返回结果

获取 URL 的参数

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

const http = require('http');
const url = require('url');
const util = require('util');

http.createServer(function (req, res){
	res.writeHead(200, {'Content-Type': 'text/plain; charset=utf-8'});
	
	// 解析 url 参数
	var params = url.parse(req.url, true).query;
	res.write("网站名:" + params.name);
	res.write("\n");
	res.write("网站 URL:" + params.url);
	res.end();
}).listen(8888);

使用 node 命令执行以上的代码:

node server.js

在浏览器中访问 http://localhost:8888/user?name=码云笔记&url=www.mybj123.com 然后查看返回结果:

获取 URL 的参数

获取 POST 请求内容

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

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

基本语法结构说明

const http = require('http');
const querystring = require('querystring');
const util = require('util');
 
http.createServer(function(req, res){
    // 定义了一个 post 变量,用于暂存请求体的信息
    const post = '';     
 
    // 通过 req 的 data 事件监听函数,每当接受到请求体的数据,就累加到 post 变量中
    req.on('data', function(chunk){    
        post += chunk;
    });
 
    // 在 end 事件触发后,通过 querystring.parse 将 post 解析为真正的 POST 请求格式,然后向客户端返回。
    req.on('end', function(){    
        post = querystring.parse(post);
        res.end(util.inspect(post));
    });
}).listen(8888);

实例演示

以下实例表单通过 POST 提交并输出数据:

const http = require('http');
const querystring = require('querystring');
 
const postHTML = 
  '<html><head><meta charset="utf-8"><title>码云笔记 Node.js 实例</title></head>' +
  '<body>' +
  '<form method="post">' +
  '网站名: <input name="name"><br>' +
  '网站 URL: <input name="url"><br>' +
  '<input type="submit">' +
  '</form>' +
  '</body></html>';
 
http.createServer(function (req, res) {
  let body = "";
  req.on('data', function (chunk) {
    body += chunk;
  });
  req.on('end', function () {
    // 解析参数
    body = querystring.parse(body);
    // 设置响应头部信息及编码
    res.writeHead(200, {'Content-Type': 'text/html; charset=utf8'});
 
    if(body.name && body.url) { // 输出提交的数据
        res.write("网站名:" + body.name);
        res.write("<br>");
        res.write("网站 URL:" + body.url);
    } else {  // 输出表单
        res.write(postHTML);
    }
    res.end();
  });
}).listen(8888);

使用 node 命令执行以上的代码:

node server.js

执行结果 Gif 演示:

获取 POST 请求内容

「点点赞赏,手留余香」

0

给作者打赏,鼓励TA抓紧创作!

微信微信 支付宝支付宝

还没有人赞赏,快来当第一个赞赏的人吧!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。
码云笔记 » Node.js GET/POST请求

发表回复