簡體   English   中英

這個 PUT 語句有什么問題?

[英]What is wrong with this PUT statement?

我正在嘗試使用 Nodejs 和 Express 編寫 PUT 語句,但失敗了。 我在這里包含了整個錯誤消息。 還有一個 GET 應用程序,但它更簡單並且似乎可以按預期工作。

服務器.js

app.use('/data', express.static('/views/data'));


app.put('/data/1-1', (req, res) => {
  let body = "";
  req.on('data', function (chunk) {
    body += chunk;
  });
  
  req.on('end', function () {
    do something  
  });
}); 

命令行

curl -X PUT http://localhost/data/1-1 -H "Accept: application/json" -H "Content-Type: application/json" --data-binary '[{"key": 1}]'

錯誤

ReferenceError: data is not defined
    at app.put (/home/w/server.js:172:15)
    at Layer.handle [as handle_request] (/home/w/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/w/node_modules/express/lib/router/route.js:137:13)
    at Route.dispatch (/home/w/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/w/node_modules/express/lib/router/layer.js:95:5)
    at /home/w/node_modules/express/lib/router/index.js:281:22
    at Function.process_params (/home/w/node_modules/express/lib/router/index.js:335:12)
    at next (/home/w/node_modules/express/lib/router/index.js:275:10)
    at /home/w/server.js:146:5
    at Layer.handle [as handle_request] (/home/w/node_modules/express/lib/router/layer.js:95:5)

使用express ,您可以訪問req.body中的請求正文。

所以在你的情況下:

app.put('/data/1-1', (req, res) => {
  console.log(req.body);
  // do something
  res.send(''); // sends an empty response
}); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM