如何从Node.js中的HTTP post方法中提取表单数据(form[method="post"])和文件上传?
我看了文件,谷歌了一下,什么都没找到。
function (request, response) {
//request.post????
}
有图书馆或黑客吗?
如何从Node.js中的HTTP post方法中提取表单数据(form[method="post"])和文件上传?
我看了文件,谷歌了一下,什么都没找到。
function (request, response) {
//request.post????
}
有图书馆或黑客吗?
当前回答
有很多种方法。然而,我所知道的最快的方法是使用带有body-parser的Express.js库。
var express = require("express");
var bodyParser = require("body-parser");
var app = express();
app.use(bodyParser.urlencoded({extended : true}));
app.post("/pathpostdataissentto", function(request, response) {
console.log(request.body);
//Or
console.log(request.body.fieldName);
});
app.listen(8080);
这可以用于字符串,但我要更改bodyParser。urlencoded to bodyParser。如果POST数据包含json数组,则改为json。
更多信息:http://www.kompulsa.com/how-to-accept-and-parse-post-requests-in-node-js/
其他回答
如果你使用node- terrible,你可以这样做:
var formidable = require("formidable");
var form = new formidable.IncomingForm();
form.parse(request, function (err, fields) {
console.log(fields.parameter1);
console.log(fields.parameter2);
// ...
});
如果你从POST接收到JSON格式的数据。:
import http from 'http';
const hostname = '127.0.0.1';
const port = 3000;
const httpServer: http.Server = http.createServer((req: http.IncomingMessage, res:
http.ServerResponse) => {
if(req.method === 'POST') {
let body: string = '';
req.on('data',(chunck) => {
body += chunck;
});
req.on('end', () => {
const body = JSON.parse(body);
res.statusCode = 200;
res.end('OK post');
});
}
});
httpServer.listen(port, hostname, () => {
console.info(`Server started at port ${port}`);
})
表达v4.17.0
app.use(express.urlencoded( {extended: true} ))
console.log(req.body) // object
演示的形式
另一个答案
您可以使用“request - Simplified HTTP client”和Javascript Promise轻松地发送和获取POST请求的响应。
var request = require('request');
function getData() {
var options = {
url: 'https://example.com',
headers: {
'Content-Type': 'application/json'
}
};
return new Promise(function (resolve, reject) {
var responseData;
var req = request.post(options, (err, res, body) => {
if (err) {
console.log(err);
reject(err);
} else {
console.log("Responce Data", JSON.parse(body));
responseData = body;
resolve(responseData);
}
});
});
}
如果你使用Express (Node.js的高性能、高级web开发),你可以这样做:
HTML:
<form method="post" action="/">
<input type="text" name="user[name]">
<input type="text" name="user[email]">
<input type="submit" value="Submit">
</form>
API客户端:
fetch('/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
user: {
name: "John",
email: "john@example.com"
}
})
});
Node.js:(自Express v4.16.0起)
// Parse URL-encoded bodies (as sent by HTML forms)
app.use(express.urlencoded());
// Parse JSON bodies (as sent by API clients)
app.use(express.json());
// Access the parse results as request.body
app.post('/', function(request, response){
console.log(request.body.user.name);
console.log(request.body.user.email);
});
Node.js:(对于Express <4.16.0)
const bodyParser = require("body-parser");
/** bodyParser.urlencoded(options)
* Parses the text as URL encoded data (which is how browsers tend to send form data from regular forms set to POST)
* and exposes the resulting object (containing the keys and values) on req.body
*/
app.use(bodyParser.urlencoded({
extended: true
}));
/**bodyParser.json(options)
* Parses the text as JSON and exposes the resulting object on req.body.
*/
app.use(bodyParser.json());
app.post("/", function (req, res) {
console.log(req.body.user.name)
});