【问题标题】:Is there something like angular $watch in node js节点 js 中是否有类似 angular $watch 的东西
【发布时间】:2016-08-10 23:25:56
【问题描述】:

我是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 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);
    });
}

节点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;
});

【问题讨论】:

  • 这就是上传中间件中的回调正在做的事情。 res.json({error_code:0, err_desc:null}); 是成功的文件上传处理程序。
  • @Paul 所以res.json({error_code:0, err_desc:null}); 我必须调用函数''child",类似于res.child?
  • 我会写一个答案。
  • @Paul 谢谢你,我很感激

标签: javascript angularjs node.js


【解决方案1】:

有很多方法可以为此组织您的代码,但本质上您需要在上传处理程序的回调中调用您的 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});
    });
});

【讨论】:

  • POST mydomain:3000/demo net::ERR_CONNECTION_REFUSED(anonymous function) @ Error status: 0. 有被拒绝的帖子,看起来已经上传了
  • demo不是你上面定义的路由吗?
  • 完全没有,我稍微修改了你的功能,请参阅更新后的帖子。非常感谢你的帮助,你真的帮助了我
  • 哎呀,将 xls 解析为 json 函数只返回 [。怎么了?我使用time 通过命令行检查了通常需要 5 到 8 秒的解析时间,这可能是问题所在吗?
  • setTimeout(10000);修复
猜你喜欢
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-09-29
  • 2016-10-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多