Node.js没有收到POST正文

Tyl*_*ler 2 node.js express angularjs

我正在使用angularjs发送POST,如下所示:

$http.post("/mypath", { data: "foobar" })
Run Code Online (Sandbox Code Playgroud)

在nodejs(expressjs)中,我试图像这样拾取它:

app.post "/mypath", (req, res) ->
  console.log "req.body: ", req.body
  res.end()
Run Code Online (Sandbox Code Playgroud)

我尝试了各种不同的化身(body: "foobar",等等),但我不断req.body: undefined

有没有一种简单的方法可以读取节点/表达式中的有效负载?

Rir*_*ron 6

要从Node中的POST获取数据,您需要使用主体解析器。例如:

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

//use bodyParser() to let us get the data from a POST
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
Run Code Online (Sandbox Code Playgroud)

  • 我试过了,但我仍然得到 `{}` 作为主体。为什么? (4认同)