GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • 首页
  • HTML
  • JavaScript
  • CSS
  • React
  • Python3
  • Java
  • C
  • C++
  • C#
  • Go
  • SQL
  • Linux
  • VS Code

This view

  • Original page
  • New address

GeezerVison is showing www.runoob.com — Node.js GET/POST请求 | 菜鸟教程

菜鸟教程 -- 学的不仅是技术,更是梦想!

  • Vue3 教程
  • Vue2 教程
  • Bootstrap3
  • Bootstrap4
  • Bootstrap5
  • 机器学习
  • PyTorch
  • TensorFlow
  • Sklearn
  • NLP
  • AI Agent
  • Ollama
  • Coding Plan
Node.js 教程
Node.js 教程 Node.js 概念 Node.js 安装配置 Node.js VS Code Node.js NVM Node.js 创建第一个应用 Node.js 工作机制 NPM 使用介绍 PNPM 使用介绍 Node.js REPL Node.js 异步编程 Node.js 回调函数 Node.js Promise Node.js async/await Node.js 事件循环 Node.js EventEmitter Node.js Buffer Node.js Stream Node.js 模块系统 Node.js 模块导入 Node.js 函数 Node.js 路由 Node.js 全局对象 Node.js 常用工具 Node.js 文件系统 Node.js 内置模块 Node.js 测验

Node.js 应用

Node.js 构建简单应用 Node.js GET/POST请求 Node.js util 模块 Node.js Web 模块 Node.js Express 框架 Node.js RESTful API Node.js 多进程 Node.js JXcore 打包 Node.js MySQL Node.js MongoDB npx 入门教程
Node.js 文件系统
Node.js Buffer(缓冲区)

Node.js GET/POST请求

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

表单提交到服务器一般都使用 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; charset=utf-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=菜鸟教程&url=www.runoob.com 然后查看返回结果:

获取 URL 的参数

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

实例

const http = require('http'); http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-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=菜鸟教程&url=www.runoob.com 然后查看返回结果:

URL 对象

以下是 URL 对象的属性和方法:

属性/方法描述示例输出
href完整的 URL 字符串"http://example.com:8080/path?foo=bar#section"
originURL 的源,包括协议、主机名和端口号(如果存在)"http://example.com:8080"
protocolURL 的协议部分,后面带有 :"http:"
host主机名和端口号"example.com:8080"
hostname主机名,不包含端口号"example.com"
port端口号(如果指定)"8080"
pathname路径名部分"/path"
search查询字符串部分,包含开头的 ?"?foo=bar&hello=world"
searchParamsURLSearchParams 对象,用于操作查询参数{ foo: 'bar', hello: 'world' }
hash锚点部分,包含 #"#section"
方法描述示例代码示例输出
get(name)获取指定名称的查询参数值myUrl.searchParams.get("foo")"bar"
append(name, value)向查询字符串中追加新的参数myUrl.searchParams.append("newKey", "newValue")?foo=bar&newKey=newValue
set(name, value)设置查询字符串中指定名称的参数(若已存在则更新)myUrl.searchParams.set("foo", "newBar")?foo=newBar
delete(name)删除指定名称的查询参数myUrl.searchParams.delete("foo")?hello=world
has(name)判断查询字符串中是否存在指定名称的参数myUrl.searchParams.has("foo")true 或 false
forEach(callback)遍历查询参数的键值对,并执行回调函数myUrl.searchParams.forEach((v, k) => console.log(k, v))键值对输出
toString()将查询参数转换为字符串形式,适合重新构建 URLmyUrl.searchParams.toString()"foo=bar&hello=world"

实例

const myUrl = new URL("http://example.com/path?foo=bar&hello=world");

console.log(myUrl.pathname);            // 输出: /path
console.log(myUrl.searchParams.get("foo")); // 输出: bar

myUrl.searchParams.append("newKey", "newValue");
console.log(myUrl.href);               // 输出: http://example.com/path?foo=bar&hello=world&newKey=newValue

获取 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) => {
    // 检查请求方法是否为 POST
    if (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');
});

代码说明:

  1. 检测请求方法:通过 req.method === 'POST' 来判断请求类型是否为 POST。
  2. 接收数据:在 req.on('data') 事件中累加数据块,形成完整的数据体。
  3. 完成接收:在 req.on('end') 事件中处理完整的 POST 数据。
  4. 响应客户端:完成数据接收和处理后,向客户端发送响应。

你可以使用 curl 命令来测试 POST 请求:

curl -X POST -d "name=example&age=25" 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(例如表单提交),数据形式类似于 name=example&age=25。在接收到数据后,可以使用 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 = '<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) { var 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(3000);

执行结果 Gif 演示:

AI 思考中...
Node.js 文件系统
Node.js Buffer(缓冲区)

点我分享笔记

取消

  • 昵称 (必填)
  • 邮箱 (必填)
  • 引用地址
分类导航
  • Python / 数据科学
    • Python 教程
    • Python2.x 教程
    • FastAPI 教程
    • Flask 教程
    • Django 教程
    • NumPy 教程
    • Pandas 教程
    • SciPy 教程
    • Matplotlib 教程
    • Dash 教程
    • Jupyter Notebook 教程
    • Pillow 教程
    • 量化交易
    • R 教程
    • Julia 教程
  • AI / 智能开发
    • AI Agent(智能体)
    • AI(人工智能)
    • AI 应用案例
    • Codex 教程
    • Vibe Coding 教程
    • Claude Code
    • OpenCode
    • Skills(技能)
    • Ollama 教程
    • Hermes Agent
    • DeepSeek Harness
    • Pi Agent
    • AI 数学基础
    • TensorFlow 教程
    • PyTorch 教程
    • Scikit-learn 教程
    • 机器 教程
    • LangChain
    • 自然语言处理 NLP
    • OpenCV 教程
    • Selenium 教程
    • Playwright 教程
  • 前端开发
    • HTML 教程
    • HTML5 教程
    • CSS 教程
    • CSS3 教程
    • JavaScript 教程
    • HTML DOM 教程
    • TypeScript 教程
    • AJAX 教程
    • JSON 教程
    • Tailwind CSS 教程
    • Bootstrap4 教程
    • Bootstrap5 教程
    • Bootstrap6 教程
    • Foundation 教程
    • Vue.js 教程
    • Vue3 教程
    • React 教程
    • Next.js 教程
    • Three.js 教程
    • AngularJS 教程
    • Angular 教程
    • jQuery 教程
    • jQuery UI 教程
    • jQuery EasyUI 教程
    • ECharts 教程
    • Chart.js 教程
    • Highcharts 教程
    • Google 地图 教程
    • SVG 教程
    • Font Awesome 教程
  • 后端开发
    • Node.js 教程
    • Electron 教程
    • PHP 教程
    • Java 教程
    • Go 教程
    • Rust 教程
    • C# 教程
    • Unity 教程
    • Servlet 教程
    • JSP 教程
    • ASP 教程
    • AppML 教程
    • VBScript 教程
    • Swagger 教程
    • RESTful API 教程
    • Docker 教程
    • Linux 教程
    • ZooKeeper 教程
  • 数据库
    • SQL 教程
    • MySQL 教程
    • PostgreSQL 教程
    • SQLite 教程
    • MongoDB 教程
    • Redis 教程
    • Memcached 教程
  • 移动开发
    • Android 教程
    • Flutter 教程
    • Ionic 教程
    • jQuery Mobile 教程
    • Swift 教程
    • Kotlin 教程
  • DevOps / 工程化
    • Git 教程
    • SVN 教程
    • CMake 教程
    • Maven 教程
    • VS Code 教程
    • Obsidian 教程
    • PyCharm 教程
    • Eclipse 教程
    • Markdown 教程
    • LaTeX 教程
  • 编程语言
    • C 教程
    • C++ 教程
    • Zig 教程
    • Scala 教程
    • Ruby 教程
    • Perl 教程
    • Lua 教程
    • Dart 教程
    • 汇编语言 教程
    • Verilog 教程
  • 计算机基础
    • 计算机组成原理
    • 数据结构与算法
    • C 语言数据结构与算法
    • 设计模式
    • Python 设计模式
    • 正则表达式
    • HTTP 教程
    • TCP/IP 教程
    • 网络协议
    • W3C 教程
  • XML / Web Service
    • XML 教程
    • DTD 教程
    • XML DOM 教程
    • XSLT 教程
    • XPath 教程
    • XQuery 教程
    • XLink 教程
    • XPointer 教程
    • XML Schema 教程
    • XSL-FO 教程
    • Web Service 教程
    • WSDL 教程
    • SOAP 教程
    • RSS 教程
    • RDF 教程
  • .NET
    • ASP.NET 教程
    • MVC 教程
    • Razor 教程
    • Web Forms 教程
    • Web Pages 教程
    • PowerShell 教程
  • 网站建设
    • 网站建设指南
    • 浏览器信息
    • 网站主机教程
    • 网站品质

字节 Coding Plan 限时 9.9 元起,支持 Deepseek-V4、GLM-5.3、Doubao-Seed-Evolving、Kimi-K3。
百度 Dumate 百度推出的桌面级 AI 智能体。 输入激活码送 4000 积分
使用文档
Trae AI 原生开发环境,让编程更高效。新用户注册赠2500积分,管理项目,也交付成果。
Advertisement
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.