【发布时间】:2021-05-27 02:52:45
【问题描述】:
我在 nodejs / express 中设置了这样的路由:
const testSync = (req, res) => {
res.status(200).send(req.body.url)
}
router.post('/test-sync', testSync);
在我的客户端中,我使用 fetch 发出 api 请求,如下所示:
const requestOptions = {
method: 'POST',
redirect: 'follow',
body: {
url: "some url"
}
};
fetch(`http://${domainName}/api/test-sync`, requestOptions)
.then(response => response.text())
.then(result => {
console.log(result)
})
.catch(error => {
console.log(error)
});
我这样做是为了测试如何使用请求正文数据。到目前为止,如果我在邮递员上使用这个 api 端点,我只会得到一个空的 json。如果我想访问请求正文数据,我应该怎么做?就像我的代码中的req.body.url。
【问题讨论】: