我想知道如何处理 Node.JS 中的 POST 请求
I want to know how to handle POST requests in Node.JS
好的,所以我希望应用程序在端口 8080 或 80 向我的服务器发送 POST 请求。我不知道如何让 Node 处理这些请求。我希望能够将请求的正文打印到控制台中。
您需要如下创建一个http服务器并检查请求方法。
看看Nodejs Http Server
为了解剖 http transactions
您可以使用 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);
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
//Create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.post('/user', urlencodedParser, function(req,res) {
console.log(req.body.login);
console.log(req.body.password);
});
app.listen(8080);
其中 login 和 password 是我从表单
获得的输入名称
好的,所以我希望应用程序在端口 8080 或 80 向我的服务器发送 POST 请求。我不知道如何让 Node 处理这些请求。我希望能够将请求的正文打印到控制台中。
您需要如下创建一个http服务器并检查请求方法。 看看Nodejs Http Server
为了解剖 http transactions
您可以使用 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);
var express = require('express');
var bodyParser = require('body-parser');
var app = express();
//Create application/x-www-form-urlencoded parser
var urlencodedParser = bodyParser.urlencoded({ extended: false });
app.post('/user', urlencodedParser, function(req,res) {
console.log(req.body.login);
console.log(req.body.password);
});
app.listen(8080);
其中 login 和 password 是我从表单
获得的输入名称