javascript - 我想知道如何在 Node.JS 中处理 POST 请求
问题描述
好的,所以我希望应用程序在端口 8080 或 80 向我的服务器发送 POST 请求。我不知道如何让 Node 处理这些请求。我希望能够将请求的正文打印到控制台中。
解决方案
您需要如下创建一个 http 服务器并检查请求方法。看看Nodejs Http Server
你可以使用express来处理这些操作
var http = require('http');
http.createServer(function (req, res) {
if (req.method == 'POST') {
let body = [];
req.on('data', (chunk) => {
body.push(chunk);
}).on('end', () => {
body = Buffer.concat(body).toString();
// at this point, `body` has the entire request body stored in it as a string
console.log(body);
});
}
}).listen(8080);
推荐阅读
- javascript - 如何禁用通过透明粘顶菜单查看元素
- javascript - 给定时间和当前时间之间的进度条
- pyiron - 如何访问 pyiron 中的净位移
- azure-devops - 编写 Azure DevOps 构建计划脚本会产生意外结果
- flutter - 根路由和不同的 initialRoute 创建奇怪的 WidgetTree
- android - 在真实设备上运行时,我的应用不会将数据上传到 Firebase
- android - RecyclerView 不显示所有带有 ScrollView 的项目,并且中间项目使用 NestedScrollView 拉伸
- class - 函数结束后对数据成员的修改不会持续存在
- ruby - Vagrantfile:将内联shell脚本的结果分配给变量
- javascript - 创建打字稿类型“接口之一”