admin管理员组文章数量:1287511
Hey I am trying to send the post request using express and node and here is my code.
index.html
<html>
<head>
<title>Test</title>
</head>
<body>
<form action="/form" method="POST" enctype="multipart/form-data">
<input type="text" name="imagename"></input>
<input type="submit" name="submit" value="submit"></input>
</form>
</body>
</html>
My app.js file is given below:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.post('/form', function(req, res){
res.setHeader('Content-Type', 'application/json');
setTimeout(function(){
res.send(JSON.stringify({
imagename: req.body.imagename || null
}));
}, 1000);
});
Now I should get the output as imagename: //value added in the form if true or else null. And I am always getting a null value. I tried to log the value of req.body.imagename and I am getting undefined instead of the value that I inserted in the form. Any help would be appretiated.
Hey I am trying to send the post request using express and node and here is my code.
index.html
<html>
<head>
<title>Test</title>
</head>
<body>
<form action="/form" method="POST" enctype="multipart/form-data">
<input type="text" name="imagename"></input>
<input type="submit" name="submit" value="submit"></input>
</form>
</body>
</html>
My app.js file is given below:
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.post('/form', function(req, res){
res.setHeader('Content-Type', 'application/json');
setTimeout(function(){
res.send(JSON.stringify({
imagename: req.body.imagename || null
}));
}, 1000);
});
Now I should get the output as imagename: //value added in the form if true or else null. And I am always getting a null value. I tried to log the value of req.body.imagename and I am getting undefined instead of the value that I inserted in the form. Any help would be appretiated.
Share Improve this question edited Jul 10, 2017 at 17:16 M.Shaikh asked Jul 10, 2017 at 16:55 M.ShaikhM.Shaikh 2501 gold badge4 silver badges14 bronze badges2 Answers
Reset to default 7You have to add the body-parser
to your Express's app.
var app = express();
var bodyParser = require('body-parser');
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }));
// parse application/json
app.use(bodyParser.json());
However, I've seen that you've declared your form with the enctype='multipart/formdata'
. This is usually used to make file uploads, if it's really what you want, you're going to need to use another parser for it.
You required 'body-parser'
middleware, but forgot to use it in your express
app.
app.use(bodyParser.urlencoded())
and you don't need this one
enctype="multipart/form-data"
本文标签: javascriptGetting http post form data in NodeJSStack Overflow
版权声明:本文标题:javascript - Getting http post form data in Node.JS - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741256024a2366687.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论