I.はじめに
- githubの
- Node.jsの身体分析ミドルウェア
- 着信要求の本体のミドルウェア解析(応答体)で、プログラムを処理する前に
-
body-parser
オファー4つのパーサJSONボディパーサーの生の体パーサテキストパーサ本体ボディで符号化された形式パーサのURL
第二に、使用
簡単なデモを構築
MKDIRボディparser- デモ
CD本体 -parser- デモ
NPMのinit - Y
NPMインストール特急体 -parser --save
新index.js
VaRのエクスプレス=(必要' 発現' )
VAR bodyParser =必要とする(' ボディパーサー' )
CONST localPortでは= 3000
VARアプリ= 表現()
// アプリケーションを作成/ JSONパーサ
VAR jsonParser = bodyParser.json()
// アプリケーションを作成/ x-www-form-urlencodedでパーサ
VAR urlencodedParser = bodyParser.urlencoded({拡張:偽})
app.post(' /login.do '、(REQ、RES)=> {
にconsole.log('******************** ' )
はconsole.log(req.body)
res.end();
})
app.listen(localPortでは、() => {
にconsole.log(' http://127.0.0.1:%s ' 、ホスト、ポート)
})
実行node index.js,
用にネットワークシミュレーション要求をPostman
ツール
ミドルウェア、直接アクセスを使用しないでください。body为undefined
1、JSONパーサー
app.post(' /login.do '、jsonParser、(REQ、RES)=> {
にconsole.log(' ******************** ' )
コンソール。ログ(req.body)
res.end();
})
注:シミュレータ以外の場合はJSON
、フォーマットの送信、空得ることができますJSON
ターゲットを
urlencoded解析器即将上述代码的 jsonParser 换成 urlencodedParser
即可
2、加载到没有挂载路径的中间件
// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))
// parse application/json
app.use(bodyParser.json())