bodyparser

今天在用bodyparser時,遇到了以下問題:git

首先科普下bodyparser的做用:它用於解析客戶端請求的body中的內容,內部使用JSON編碼處理,url編碼處理以及對於文件的上傳處理。github

如今繼續說下上面截圖問題的意思:意爲express

由於新版的express中不包含bodyparser,須要咱們單獨安裝bodyparser。
解決方案:
使用npm install body-parser安裝body-parser,而後在app.js中加載body-parser模塊var bodyParser = require('body-parser'),把app.use(express.bodyParser())替換成app.use(bodyParser.urlencoded({extended:false})),這樣就ok了。
 
可是還有一點要注意,在用post提交數據時須要把參數extended:false改成extended:true,不然會報錯。
爲啥會報錯呢,由於一般post內容的格式爲application/x-www-form-urlencoded,所以要用下面的方式來使用:app.use(require('body-parser').unlencoded({extended:true}))
 
詳情見https://github.com/expressjs/body-parser
 
上面是我遇到問題,在慕課問答中找到答案總結的,但願能給你們帶來幫助。
相關文章
相關標籤/搜索