Javascript 在NodeJS中是否有类似angular$watch的东西
我是Web服务方面的新手,对node js了解不深,因此如果问题不正确,我提前表示歉意。 我的问题是我的angular node js应用程序中有两个函数。 第一个功能,将文件上载到服务器上的公用文件夹Javascript 在NodeJS中是否有类似angular$watch的东西,javascript,angularjs,node.js,Javascript,Angularjs,Node.js,我是Web服务方面的新手,对node js了解不深,因此如果问题不正确,我提前表示歉意。 我的问题是我的angular node js应用程序中有两个函数。 第一个功能,将文件上载到服务器上的公用文件夹 var storage = multer.diskStorage({ //multers disk storage settings destination: function (req, file, cb) { cb(null, './demo/'); },
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});
});
});
第二个函数,调用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);
});
}
在NodeJS中是否有类似angular$watch的东西,我可以将其设置为文件上载函数,因此如果文件已成功上载,请调用java函数
由@Paul提供的解决方案(已修改)
有很多方法可以组织代码,但本质上需要在上传处理程序的回调中调用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});
});
});
这就是上传中间件中的回调所做的。其中有
res.json({error\u code:0,err\u desc:null})
这是成功的文件上载处理程序。@Paul so改为res.json({error\u code:0,err\u desc:null})代码>我必须调用函数“child”,类似这样的res.child
?我会写一个答案。@Paul谢谢,我很感激它发布net::ERR\u CONNECTION\u densed(匿名函数)@Error status:0.有一个被拒绝的帖子,看起来它已经被上传了。Demo不是你上面定义的路由吗?一点也不是,我稍微修改了你的函数,请参阅更新的帖子。非常感谢你的帮助,你真的帮助了meoops,而不是将xls解析为json函数返回[
。怎么了?我用时间通过命令行检查,通常解析需要5到8秒,这可能是问题所在吗?用设置超时(10000)修复;
/** 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});
});
});