express.js - body-parser 解析請求內容
Commit
npm install body-parser
程式碼
var express = require('express');
var bodyParser = require('body-parser')
var app = express()
.use( bodyParser() ).use(function (req, res) {
console.log("body", req.body)
console.log("foo", req.body.foo)
res.send(req.body)
})
.listen(3000);
$ curl -s http://127.0.0.1:3000/ -H "content-type: application/json" -d "{\"foo\":123}"
{"foo":123}