简体   繁体   English

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

[英]Is there something like angular $watch in node js

I'm rookie in Web services and my knowledges are not so deep in node js, so I apologize in advance if the question is not correct.我是 Web 服务的菜鸟,我在 node js 方面的知识不是很深,所以如果问题不正确,我提前道歉。 My Question is I have two functions in my angular-node js app.我的问题是我的 angular-node js 应用程序中有两个函数。 First function, uploading file to 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 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});
    });
});

Second funciton, calling executive java app第二个函数,调用执行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);
    });
}

Is there something like angular $watch in node js that I could set it on file upload function, so if the file has been successfully upload call java function node js中是否有类似angular $watch之类的东西,我可以在文件上传功能上设置它,所以如果文件已成功上传调用java函数

Solution provided by @Paul (modified) @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;
});

There're a lot of ways to organize your code for this, but essentially you need to call your java function inside the callback for your upload handler.有很多方法可以为此组织您的代码,但基本上您需要在上传处理程序的回调中调用您的 java 函数。 Something like this should work:这样的事情应该工作:

 /** 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM