Express PUT接口中req.body返回Object导致文件写入类型错误的问题求助
Express PUT接口中req.body返回Object导致文件写入类型错误的问题求助
我现在碰到个棘手的问题:req.body 返回的是一个Object类型,运行下面的代码时直接触发了TypeError,感觉req变量里根本没拿到我传的有效内容。
首先说下我用来测试的curl命令,我想把字符串here-again写入到/home/dave/test.txt文件里,试过给here-again加引号,但还是没用:
curl -X PUT http://localhost:8008/config/ --data here-again
这是我的Node.js代码:
app.put('/config/', function (req, res) { const filename = '/home/dave/test.txt'; const data = req.body; //res.send('body ' + data + ' ' + filename ); console.log(data) fs.writeFile(filename, data, (err) => { if (err) { console.error(err); res.status(500).send('Error writing file'); } else { res.send('Data saved successfully'); } }); })
最后是我收到的错误信息:
TypeError [ERR_INVALID_ARG_TYPE]: The "data" argument must be of type string or an instance of Buffer, TypedArray, or DataView. Received an instance of Object
备注:内容来源于stack exchange,提问作者D Liebman
相关产品推荐
相关产品推荐

