在节点js中是否有类似$ $ watch的东西

我是networking服务的新手,我的知识在节点j中并不是很深,所以如果问题不正确,我就提前道歉。 我的问题是我有我的angular度节点js应用程序中的两个function。 第一个function,上传文件到服务器上的公用文件夹

var storage = multer.diskStorage({ //multers disk storage settings destination: function (req, file, cb) { cb(null, './demo/'); }, filename: function (req, file, cb) { //var datetimestamp = Date.now(); cb(null, file.originalname //file.fieldname + '-' + datetimestamp + '.' + file.originalname.split('.')[file.originalname.split('.').length -1] ); } }); var upload = multer({ //multer settings storage: storage }).single('file'); /** API path that will upload the files */ app.post('/upload', function(req, res) { upload(req,res,function(err){ if(err){ res.json({error_code:1,err_desc:err}); return; } res.json({error_code:0,err_desc:null}); }); }); 

第二个function,调用执行Java应用程序

  var child = function() { spawn('java', ['-Xms64M', '-Xms64M', '-jar', '/home/ubuntu/code/ParseExcel.jar', '/var/www/html/demo/test.xls']); child.on('close', function (exitCode) { if (exitCode !== 0) { console.error('Something went wrong!'); } }); child.stderr.on('data', function (data) { process.stderr.write(data); }); } 

有没有像节点jsangular度$ watch,我可以设置它的file uploadfunction,所以如果文件已成功上传调用java函数

由@Paul(修改)提供的解决scheme

  app.post('/upload', function(req, res) { upload(req,res,function(err){ if(err){ res.json({error_code:1,err_desc:err}); return; } // first, call your child code: var child = spawn('java', ['-Xms64M', '-Xms64M', '-jar', '/home/ubuntu/code/ParseExcel.jar', '/var/www/html/demo/test.xls']); child.on('close', function (exitCode) { if (exitCode !== 0) { console.error('Something went wrong!'); } }); child.stderr.on('data', function (data) { process.stderr.write(data); }); // In my case java app parsing xls to json around 5-8 sec, that's why I'm using timeout setTimeout(10000); // then respond to the client so it's not waiting: res.json({error_code:0,err_desc:null}); }); //return req.child; }); 

有很多方法来组织你的代码,但基本上你需要在你的上传处理程序的callback中调用你的java函数。 像这样的东西应该工作:

  /** API path that will upload the files */ app.post('/upload', function(req, res) { upload(req,res,function(err){ if(err){ res.json({error_code:1,err_desc:err}); return; } // first, call your child code: child(); // then respond to the client so it's not waiting: res.json({error_code:0,err_desc:null}); }); });