javascript - Is there something like angular $watch in node js -
i'm rookie in web services , knowledges not deep in node js, apologize in advance if question not correct. question have 2 functions in angular-node js app. first function, uploading file public folder on server
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 upload 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}); }); });
second funciton, calling executive java app
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); }); }
is there angular $watch in node js set on file upload function, if file has been upload call java function
solution provided @paul (modified)
app.post('/upload', function(req, res) { upload(req,res,function(err){ if(err){ res.json({error_code:1,err_desc:err}); return; } // first, call 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 case java app parsing xls json around 5-8 sec, that's why i'm using timeout settimeout(10000); // respond client it's not waiting: res.json({error_code:0,err_desc:null}); }); //return req.child; });
there're lot of ways organize code this, need call java function inside callback upload handler. should work:
/** api path upload 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 child code: child(); // respond client it's not waiting: res.json({error_code:0,err_desc:null}); }); });
Comments
Post a Comment