我正在使用React作为我的前端框架,Express.js作为我的后端框架。我对这两个框架都很陌生。我有一个名为Papers的组件,我试图首先通过fetch post将一些文档上传到后端服务器,然后在服务器端执行一些操作,最后从同一处理程序中获取来自服务器的结果。我的处理程序代码如下:
handleDrop(files) {
var data = new FormData();
alert((files[0]) instanceof File);
files.forEach((file, index) => {
data.append('file' + index, file);
});
fetch('/file_upload', {
method: 'POST',
body: data
});
// the second fetch would crash the app.
/*fetch('/file_upload').then(function(data){
return data.json();
}).then( json => {
this.setState({
papers: json
});
console.log(json);
});*/
}
我这里是 get 方法的 Express 后端代码:
app.get('/file_upload', function(req, res){
// testing
res.send({name:"lol"});
});
目前的问题是第二次获取数据会导致Express应用程序崩溃。由于我对React和Express不熟悉,是否有人可以给我一些提示,告诉我是否以正确的方式进行操作?谢谢!
服务器代码链接:https://github.com/kaiwenshi/learn-webpack/blob/master/src/server/index.js