我正在为 Node.js 应用程序编写一个简单的 preact 前端。我正在尝试从项目目录中的 json 文件读取数据,从用户那里获取一些输入,然后将其写回到文件中。
我可以使用 fetch() 从我的项目目录加载 json 文件...
fetch('config.json');
...但是当我将数据写回文件时,我得到了 404...
fetch('config.json', {
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
method: 'POST',
body: JSON.stringify(this.state)
});
有什么想法为什么这行不通吗?
最佳答案
发布请求需要由您的服务器处理,因此您不能直接发布到文件。
如果您使用的是 Express,则可以为此创建一条路线。像这样的东西:
app.post('/post-data', function(request, response){
// get data and write to the file here
})
关于javascript - fetch() 后出现 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47254503/