
在很多场景中我们的服务器都需要跟用户的浏览器打交道如表单提交。表单提交到服务器一般都使用 GET/POST 请求。本章节我们将为大家介绍 Node.js GET/POST请求。获取GET请求内容由于 GET 请求直接被嵌入在路径中URL 是完整的请求路径包括了 ? 后面的部分因此你可以手动解析后面的内容作为 GET 请求的参数。node.js 中 可以使用 URL 构造函数解析请求的 URL。URL 对象是 Node.js 中用于解析和操作 URL 的一种标准工具。它提供了方便的接口来访问和修改 URL 的各个组成部分如协议、主机名、路径、查询参数等。实例const http require(http); const util require(util); http.createServer((req, res) { res.writeHead(200, { Content-Type: text/plain; charsetutf-8 }); // 使用 URL 构造函数解析请求的 URL const myUrl new URL(req.url, http://${req.headers.host}); // 输出 URL 的各个部分 res.end(util.inspect({ href: myUrl.href, origin: myUrl.origin, protocol: myUrl.protocol, host: myUrl.host, hostname: myUrl.hostname, port: myUrl.port, pathname: myUrl.pathname, search: myUrl.search, searchParams: Object.fromEntries(myUrl.searchParams) // 将 searchParams 转为普通对象 })); }).listen(3000); console.log(Server is running at http://localhost:3000);在浏览器中访问http://localhost:3000/user?name菜鸟教程urlwww.runoob.com然后查看返回结果:获取 URL 的参数我们可以使用 url.parse 方法来解析 URL 中的参数代码如下实例const http require(http); http.createServer((req, res) { res.writeHead(200, { Content-Type: text/plain; charsetutf-8 }); // 使用 URL 构造函数解析请求 URL const myUrl new URL(req.url, http://${req.headers.host}); // 获取查询参数 const name myUrl.searchParams.get(name); const siteUrl myUrl.searchParams.get(url); res.write(网站名 (name || 未提供)); res.write(\n); res.write(网站 URL (siteUrl || 未提供)); res.end(); }).listen(3000); console.log(Server is running at http://localhost:3000);在浏览器中访问http://localhost:3000/user?name菜鸟教程urlwww.runoob.com然后查看返回结果:URL 对象以下是 URL 对象的属性和方法属性/方法描述示例输出href完整的 URL 字符串http://example.com:8080/path?foobar#sectionoriginURL 的源包括协议、主机名和端口号如果存在http://example.com:8080protocolURL 的协议部分后面带有:http:host主机名和端口号example.com:8080hostname主机名不包含端口号example.comport端口号如果指定8080pathname路径名部分/pathsearch查询字符串部分包含开头的??foobarhelloworldsearchParamsURLSearchParams对象用于操作查询参数{ foo: bar, hello: world }hash锚点部分包含##section方法描述示例代码示例输出get(name)获取指定名称的查询参数值myUrl.searchParams.get(foo)barappend(name, value)向查询字符串中追加新的参数myUrl.searchParams.append(newKey, newValue)?foobarnewKeynewValueset(name, value)设置查询字符串中指定名称的参数若已存在则更新myUrl.searchParams.set(foo, newBar)?foonewBardelete(name)删除指定名称的查询参数myUrl.searchParams.delete(foo)?helloworldhas(name)判断查询字符串中是否存在指定名称的参数myUrl.searchParams.has(foo)true或falseforEach(callback)遍历查询参数的键值对并执行回调函数myUrl.searchParams.forEach((v, k) console.log(k, v))键值对输出toString()将查询参数转换为字符串形式适合重新构建 URLmyUrl.searchParams.toString()foobarhelloworld实例const myUrl new URL(http://example.com/path?foobarhelloworld);console.log(myUrl.pathname); // 输出: /pathconsole.log(myUrl.searchParams.get(foo)); // 输出: barmyUrl.searchParams.append(newKey, newValue);console.log(myUrl.href); // 输出: http://example.com/path?foobarhelloworldnewKeynewValue获取 POST 请求内容在 Node.js 中处理 POST 请求通常需要通过 http 模块来接收请求体中的数据。POST 请求数据不像 GET 请求那样包含在 URL 中而是作为请求体发送。因此在 Node.js 中接收 POST 数据时需要监听并处理 request 对象的 data 和 end 事件。监听data事件当数据块到达服务器时data事件触发数据块作为回调的参数传递。监听end事件当整个请求体接收完毕时end事件触发这时可以对完整的 POST 数据进行处理。以下代码展示了如何在 Node.js 中使用 http 模块来处理 POST 请求:实例const http require(http);// 创建 HTTP 服务器http.createServer((req, res) {// 检查请求方法是否为 POSTif (req.method POST) {let body ;// 监听 data 事件逐块接收数据req.on(data, (chunk) {body chunk; // 累加接收到的数据块});// 监听 end 事件数据接收完毕req.on(end, () {// 输出接收到的 POST 数据console.log(Received POST data:, body);// 设置响应头和内容res.writeHead(200, { Content-Type: text/plain });res.end(POST data received successfully!);});} else {// 非 POST 请求的处理res.writeHead(405, { Content-Type: text/plain });res.end(Only POST requests are supported.);}}).listen(3000, () {console.log(Server is running at http://localhost:3000);});代码说明检测请求方法通过req.method POST来判断请求类型是否为 POST。接收数据在req.on(data)事件中累加数据块形成完整的数据体。完成接收在req.on(end)事件中处理完整的 POST 数据。响应客户端完成数据接收和处理后向客户端发送响应。你可以使用 curl 命令来测试 POST 请求curl -X POST -d nameexampleage25 http://localhost:3000处理 JSON 数据如果 POST 请求发送的是 JSON 数据可以在 req.on(end) 中将接收的数据解析为对象req.on(end, () { const parsedData JSON.parse(body); // 将 JSON 字符串解析为对象 console.log(Received JSON data:, parsedData); res.end(JSON data received successfully!); });querystring 模块querystring 模块用于处理 URL 查询字符串和 POST 请求的数据。对于 application/x-www-form-urlencoded 编码的 POST 请求数据querystring 模块可以帮助解析请求体将它转换成 JavaScript 对象方便访问和操作。假设客户端发送的 POST 请求数据格式为application/x-www-form-urlencoded例如表单提交数据形式类似于nameexampleage25。在接收到数据后可以使用querystring.parse方法将数据解析成对象。基本语法结构说明var http require(http); var querystring require(querystring); var util require(util); http.createServer(function(req, res){ // 定义了一个post变量用于暂存请求体的信息 var 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(3000);以下实例表单通过 POST 提交并输出数据实例var http require(http); var querystring require(querystring); var postHTML htmlheadmeta charsetutf-8title菜鸟教程 Node.js 实例/title/head body form methodpost 网站名 input namenamebr 网站 URL input nameurlbr input typesubmit /form /body/html; http.createServer(function (req, res) { var body ; req.on(data, function (chunk) { body chunk; }); req.on(end, function () { // 解析参数 body querystring.parse(body); // 设置响应头部信息及编码 res.writeHead(200, {Content-Type: text/html; charsetutf8}); if(body.name body.url) { // 输出提交的数据 res.write(网站名 body.name); res.write(br); res.write(网站 URL body.url); } else { // 输出表单 res.write(postHTML); } res.end(); }); }).listen(3000);执行结果 Gif 演示