Node js中未定义的主体

时间:2019-08-12 09:13:26

标签: html node.js google-cloud-functions google-cloud-shell

我是这方面的新手,所以请理解我。我试图将数据从HTML页面(由Google Cloud Shell制成)发送到Node.js页面(由Google Functions制成),但是当我使用命令console.log(req.body)时,我总是在日志中看到未定义的内容。这是代码

HTML :  
<form action="link" method="post">
 ID : <input type="text" id="demo3" name="id"><br>
<br>
Nome : <input type="text" id="demo" name="fname"><br>
<br>
Cognome : <input type="text"  id="demo1" name="lname"><br>
<br>
Età : <input type="text"  id="demo2" name="eta"><br>
<br>
<input type="submit"  value="Submit"> 
</form>
  

Blockquote

Node.js :
exports.helloWorld = (req, res) => {
  console.log(req.body.lname);
  console.log(req.body);
};
  

Blockquote

更新,但仍然不起作用

exports.helloWorld = (req, res) => {
    var express = require('express')
  var bodyParser = require('body-parser');

var app = express();
app.use(bodyParser.urlencoded({ extended: true }))
    console.log(req.body.fname);
  console.log(req.headers['content-type']);
    res.send(req.headers);
}; 

1 个答案:

答案 0 :(得分:-1)

这在表达

的情况下有效
const app = express();
app.use(bodyParser.urlencoded({
    extended: true
}));

已更新:

var express = require('express');
var bodyParser = require('body-parser');

var app = express();
app.use(bodyParser.urlencoded({ extended: true }))

app.get('/', function (req, res) {
   console.log(req.body.fname);
   console.log(req.headers['content-type']);
   res.send(req.headers);
})

app.listen(3000)