req.body未定义-nodejs [英] req.body is undefined - nodejs
本文介绍了req.body未定义-nodejs的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的html:
<form action='/new' method='POST' >
<span>pool name: </span>
<input type="text" name="name" />
<input type="hidden" name="imageSrcList" />
<button type='submit' >Create new</button>
</form>
这是相关的JS:
var app = express()
app.use(fileUpload());
app.set('view engine', 'ejs')
app.use(express.static(__dirname + '/views'));
app.post('/new', (req, res) => {
console.log(req.body.name);
})
控制台读取:
TypeError: Cannot read property 'name' of undefined
我已经尝试过使用console.log(req.body)
,但这也是未定义的.
I have tried with console.log(req.body)
and this also is undefined.
提前谢谢!
推荐答案
您缺少将req.body
设置为值所必需的body-parser
中间件. Express默认情况下不附带此功能,需要通过NPM npm i --save body-parser
You're missing the body-parser
middleware which is necessary to have req.body
set to a value. Express doesn't come with this by default and will need to be installed via NPM npm i --save body-parser
const bodyParser = require('body-parser')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({extended: true}))
这篇关于req.body未定义-nodejs的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文