javascript - 如何接受来自 Node.js web application.js 的 POST 请求并返回响应?
问题描述
看不懂是什么问题,这个请求的本质是传递一些数据,调用一个函数,然后返回生成的Json文件,但是这并没有发生,而是服务器什么都没有返回。可能是什么问题呢?请帮我
Web 部件
clickOnRow: function(elem, whichScreen){
this.clicks++
if(this.clicks === 1) {
var self = this
this.timer = setTimeout(function() {
console.log("одинарный");
self.clicks = 0
}, this.delay);
} else{
clearTimeout(this.timer);
console.log("двойной");
elem['whichScreen'] = whichScreen;
console.log(this.helper);
// this.nameOfMethod(elem);
this.clicks = 0;
fetch('/currentDir1',{
method: 'POST',
mode: 'cors',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(elem)
})
.then(response => response.json())
.then(json => this.helper = json)
.then(json => this.$emit("newvalue", json))
console.log("helper");
console.log(this.helper);
服务器部分
router.post('/currentDir1',(req, req) =>{
console.log("POST");
let body = "";
let pathToFile = "";
req.on("data", function (data) {
body += data;
});
req.on("end", function(currentData) {
console.log(JSON.parse(body));
currentData = JSON.parse(body);
if(currentData.sizeOrType === "<папка>"){
let dir = currentData.dir + currentData.fileName;
// dir = "C:\\totalcmd";
console.log(dir);
if(currentData.whichScreen){
foo(dir, './data/firstScreen.json');
pathToFile = './data/firstScreen.json';
res.sendFile(path.resolve('./data/firstScreen.json'));
}else{
console.log('aaaa');
Foo(dir, './data/secondScreen.json');
pathToFile = './data/firstScreen.json';
res.sendFile(path.resolve('./data/secondScreen.json'));
}
}
// res.json({ message: 'goodbye'})
res.json(path.resolve(pathToFile));
});
res.sendFile(path.resolve(pathToFile));
})
解决方案
推荐阅读
- angularjs - 使用 ng-repeat 选择中的预选选项
- android - 窗口管理器阻止浮动聊天头下方应用程序的键盘
- c++ - c ++ /地图中的元素通过退出函数神秘地消失
- python - NameError:未定义名称“openFile”
- sql - 将多行数据放入单行
- postgresql - postgres 的设置:更新现有数据库或创建新数据库
- django - 如何在 Django 会话中存储用户的所有信息
- c - 为什么 exec() 系列函数实际上返回一个值?
- android - 删除recyclerview android中的项目时失去辅助功能焦点
- flutter - 如何在颤动中将数据从表单传递到下一个屏幕?