服务完成后,Node JS API: Return JSON 给用户。

Node JS API: Return JSON to user, after service is done.

我目前正在使用 node.js 创建 post-上传 API,以上传由 Watson Visual Recognition 服务处理的图像。此 returns 一个 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');
});

你可以使用这个

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