[英]Is there something like angular $watch in node js
我是 Web 服務的菜鳥,我在 node js 方面的知識不是很深,所以如果問題不正確,我提前道歉。 我的問題是我的 angular-node js 應用程序中有兩個函數。 第一個功能,將文件上傳到服務器上的公共文件夾
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});
});
});
第二個函數,調用執行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);
});
}
node js中是否有類似angular $watch之類的東西,我可以在文件上傳功能上設置它,所以如果文件已成功上傳調用java函數
@Paul 提供的解決方案(已修改)
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;
});
有很多方法可以為此組織您的代碼,但基本上您需要在上傳處理程序的回調中調用您的 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});
});
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.