I am working on a board with express 2.11.3 and node v0.12.7 running on it.
There is a server in node.js and I would like to retrieve data from a POST form. For the moment, "req.body.quota1" is returning undefined. Below is my code.
"req.path" and "req.ip" return values. req.body returns [Object object] and req.body.quota1 returns undefined.
Curl command line seems to work with the app curl -d "quota1=2" -X POST http://ip:8080/submitParams
. I mean, that req.body.quota1 returns 2.
What is stopping my code from retrieving data from the post form ?
I would be grateful for any help.
Best regards,
JG2
index.js
var path = require('path')
var bodyParser = require('body-parser');
var http = require('http');
var express = require('express')
var exphbs = require('express-handlebars')
var app = express()
app.use(bodyParser.urlencoded({
extended: false
}))
app.use(bodyParser.text({type: 'text/html'}));
app.use(bodyParser.json());
app.use(express.static(path.join(__dirname, '/assets')));
app.engine('.hbs', exphbs({
defaultLayout: 'layout',
extname: '.hbs',
layoutsDir: path.join(__dirname),
partialsDir: path.join(__dirname)
}))
app.set('view engine', '.hbs')
app.set('views', path.join(__dirname))
var server = app.listen(8080);
require('./user').init(app);
./user/init.js
function initUser(app) {
app.get('/', renderWelcome);
app.post('/submitParams',submitParams);
}
function submitParams(req,res)
{
console.log("req.ip: " + req.ip);
console.log(req.path);
console.log("req.body: " + req.body);
console.log("req.body.quota1: " + req.body.quota1);
res.render('user/welcome',{
...
})
}
module.exports = initUser
HTML
<form action="/submitParams" method="post" style="width:200px;">
<label for="quota1">Quota 1</label>
<input name="quota1" type="text" value={{quota1}}>
<button type="submit">Submit new quotas</button>
</form>
question from:
https://stackoverflow.com/questions/65887891/express-nodejs-post-req-body-undefined 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…