Node JS API:在服务完成后将JSON返回给用户。

时间:2017-02-14 09:15:02

标签: javascript json node.js watson

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

这是我的代码:

// 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');
});

1 个答案:

答案 0 :(得分:2)

您可以使用此

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