节点js要等到响应到来

时间:2018-03-22 09:48:13

标签: node.js

copy_deliverable_script_tomaster(args.Software_name.value,function(state){
  res.end("added")
  }
)

function copy_deliverable_script_tomaster(software_name,callback){
  client.scp('./Temporary_software_files/folder/', {
  host: 'ip',
  username: 'centos',
  privateKey: String(require("fs").readFileSync('./foreman_keypairs/coe- 
  central.pem')),
  path: '/home/centos/Software_with_files/'+software_name
  }, function(err,response) {

  if(err){
    console.log(err)
  }
  else{
    console.log("after copy in master")
    return callback(response);
}
})
}

我使用了上面的代码,将大文件复制到远程机器上。 复制文件在远程计算机中继续,但在复制完成之前响应(“无内容”)。 只有在复制完成后才会打印console.log("after copy in master")。 无法得到答复。

2 个答案:

答案 0 :(得分:0)

而不是res.end使用res.send("added");res.write('Added'); res.end();

因为你没有写任何东西就结束了回应。

copy_deliverable_script_tomaster(args.Software_name.value, function (state) {
    res.send("added")
})

答案 1 :(得分:0)

您没有处理错误情况。如果您没有在client.scp的回调中进行任何操作,则只需传递该回调即可。

copy_deliverable_script_tomaster(args.Software_name.value, function (err,state) {
    if(err) return res.status(400).send(err);
    return res.send("some response")
})

function copy_deliverable_script_tomaster(software_name, callback) {
    client.scp('./Temporary_software_files/folder/', {
                host: 'ip',
                username: 'centos',
                privateKey: String(require("fs").readFileSync('./foreman_keypairs/coe-central.pem')),
                    path: '/home/centos/Software_with_files/' + software_name
                }, callback)
            }
相关问题