Javascript 从Nodejs请求中提取POST参数
我试图通过使用以下请求从变量postData中的POST获取参数- (我之所以使用它,是因为这里有人建议-) 这里呢- 我在使用body parser之前阅读了一些解决方案,它给了我相同的错误中间件缺失,我全局安装了它,然后也得到了相同的错误,之后我阅读了关于json的内容,所以我使用Javascript 从Nodejs请求中提取POST参数,javascript,node.js,express,http-post,Javascript,Node.js,Express,Http Post,我试图通过使用以下请求从变量postData中的POST获取参数- (我之所以使用它,是因为这里有人建议-) 这里呢- 我在使用body parser之前阅读了一些解决方案,它给了我相同的错误中间件缺失,我全局安装了它,然后也得到了相同的错误,之后我阅读了关于json的内容,所以我使用 npm install -g json 那也不行。然后我尝试添加依赖项- { "name": "express_shrib.js", "version": "0.0.1", "descriptio
npm install -g json
那也不行。然后我尝试添加依赖项-
{
"name": "express_shrib.js",
"version": "0.0.1",
"description": "Creating Shrib Using Express",
"main": "express_shrib.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/iamdeadman/nodejs.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/iamdeadman/nodejs/issues"
},
"homepage": "https://github.com/iamdeadman/nodejs",
"dependencies": {
"express": ">= 1.2.0",
"json": ">= 9.0.0"
}
}
并运行了npm安装
还是同样的错误-
Error: Most middleware (like json) is no longer bundled with Express and must be installed separately. Please see https://github.com/senchalabs/connect#middleware.
at Function.Object.defineProperty.get (/home/unknown/public_html/node/node_modules/express/lib/express.js:89:13)
编辑**-使用新的主体解析器模块编写代码
var express = require('express');
var app = express();
var fs = require('fs');
var bodyParser = require('body-parser');
app.use(bodyParser());
app.post('/shrib/:file/:data',function(req,res){
var fileName = req.params.file;
var data = req.params.data;
req.on('data',function(data){ body+=data; } );
req.on('end' ,function(){
var postData = req.body;
var writeStream = fs.createWriteStream(fileName);
if(postData)
console.log(postData);
else{
console.log("failed miserably");
console.log(postData);
}
res.writeHead(200);
res.end();
});
});
app.get('/shrib/:file',function(req,res){
var fileName = req.params.file;
if(fileName != ''){
var readStream = fs.createReadStream(fileName);
var content;
readStream.on('data',function(chunk){
content+=chunk.toString();
console.log(content);
});
readStream.on('end',function(){
res.writeHead(200,{"Content-Type":"text/html"});
res.write("<form id=\"submitForm\" method=\"POST\">");
res.write("<textarea id=\"text\"rows=50 cols=50 >");
console.log(content);
if(content)
res.write(content.toString());
res.write("</textarea>");
res.write("<input type=\"submit\" value=\"submit\" />");
res.write("</form>");
res.write("<script>");
res.write("var windowLocation = location.href;");
res.write("document.getElementById(\"submitForm\").action=windowLocation + \'/data\';");
res.write("</script>");
res.end();
});
}else{
res.writeHead(200);
res.write("invalid/empty path name");
}
});
app.listen(8080);
在控制台中,这意味着body对象出于某种原因是空的。对于Express 4,body解析中间件(与其他以前内置的中间件一样)被提取到“body parser”模块中。然而,这个新模块只处理JSON和urlencoded表单提交,而不是多部分提交 如果需要多部分支持,则需要使用类似或的东西(connectmultiparty本质上是旧的expressbodyparser中间件)
编辑:此外,textarea输入字段缺少
name
属性。这是必需的,否则该字段将不会与表单一起发送。当使用express 4时,请使用主体解析器中间件获取参数。
Multipart存在创建临时文件加载的问题。因此,最好尽可能避免这种情况,直接使用上传服务
app.use(function (req, res, next) {
var urlParser = require('url');
var url = urlParser.parse(req.url, true);
if (url.pathname == "/rest/file/upload") {
next();
} else {
var contentType = req.header("content-type");
if (contentType && contentType.indexOf("application/json") != -1) {
bodyParser.json({limit: 1024 * 1024 * 10})(req, res, next);
} else {
bodyParser.urlencoded({ extended: true, limit: 1024 * 1024 * 10})(req, res, next);
}
}
});
然后只需将您的请求参数设置为:
console.log(req.param("parameter-name"));
好的,我添加了npm安装body parser-g,然后添加了app.use(bodyParser());然后尝试var postData=req.body.text;console.log(postData);但是它仍然让我在console.log()中没有定义好了,在打印req.body时我得到了{}就是这个,你能建议一下为什么文本id中的数据没有被发布吗?你可能应该将你现在使用的代码发布到
body parser
的某个地方。我编辑了body parser代码的帖子,这是我从中发现的,尽管我不明白为什么要在use函数中包装解析代码,但这是必要的吗?或者,我的做法是正确的,但我发布的回应表格是错误的
{}
app.use(function (req, res, next) {
var urlParser = require('url');
var url = urlParser.parse(req.url, true);
if (url.pathname == "/rest/file/upload") {
next();
} else {
var contentType = req.header("content-type");
if (contentType && contentType.indexOf("application/json") != -1) {
bodyParser.json({limit: 1024 * 1024 * 10})(req, res, next);
} else {
bodyParser.urlencoded({ extended: true, limit: 1024 * 1024 * 10})(req, res, next);
}
}
});
console.log(req.param("parameter-name"));