Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
641 views
in Technique[技术] by (71.8m points)

html - express nodejs post req.body undefined

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...