节点JS API:服务完成后,将JSON返回给用户. [英] Node JS API: Return JSON to user, after service is done.

查看:131
本文介绍了节点JS API:服务完成后,将JSON返回给用户.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用node.js创建上传后的API,以上传图像,该图像由Watson Visual Recognition Service处理.这将返回一个JSON,当前已将其记录到控制台. 在此过程完成之后,是否可以将JSON发送回用户? 我是Node.js的新手,非常感谢您的帮助.

I'm currently using node.js to create a post-upload API, to upload an image, which is processed by the Watson Visual Recognition Service. This returns a JSON, which is currently logged to the console. Is there a way to send this JSON back to the user, after the process is done? I'm a total newbie to Node.js, so I really appreciate your help.

这是我的代码:

// initialising ...  

app.post( '/detectFaces', avatarUpload, ( req, res ) => { 

    avatarUpload( req, res, ( err ) => {

        if ( err || !req.file )
            return res.send({ error: 'invalid_file' })

        console.log( req.file );

        var path = req.file.path;
        var name = req.file.filename;  

        var params = { 
            images_file: fs.createReadStream(path)
        };   

        //Call to the visual recognition Service
        visual_recognition.detectFaces(params, function(err, res){ 
            if(err) 
                console.log(err); 
            else  
                console.log(JSON.stringify(res, null, 2));   

        });  

        //The JSON of the visual Rec Service should send here. 
        res.send({ 'status' : 'check', url: 'uploads' + '/' + filename }) 


    }) 
    var params = { 
            images_file: fs.createReadStream(path)
        };    


}); 


app.listen(3000, function () {
  console.log('Upload Server listening on port 3000');
});

推荐答案

您可以使用

visual_recognition.detectFaces(params, function(err, result){ 
    if(err) 
        console.log(err); 
    else  
        res.status(200).json(result)   
}); 

这篇关于节点JS API:服务完成后,将JSON返回给用户.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆