首页 > 解决方案 > 我想知道如何在 Node.JS 中处理 POST 请求

问题描述

好的,所以我希望应用程序在端口 8080 或 80 向我的服务器发送 POST 请求。我不知道如何让 Node 处理这些请求。我希望能够将请求的正文打印到控制台中。

标签: javascriptnode.jshttphttp-post

解决方案


您需要如下创建一个 http 服务器并检查请求方法。看看Nodejs Http Server

对于http事务的剖析

你可以使用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);


推荐阅读