如何从路由中的节点返回从PageSpeed Insight收到的数据?

时间:2021-10-26 17:13:26

I am using the PSI npm package in a simple route. https://github.com/addyosmani/psi Used the psi method from the library. However, the script does not wait for the expected result. It works Ok in console. However, I need to return the response to browser and display it there. How can I do this? Note: psi method a Promise.

我在一个简单的路线中使用PSI npm包。 https://github.com/addyosmani/psi使用库中的psi方法。但是,脚本不会等待预期的结果。它在控制台中运行正常。但是,我需要将响应返回给浏览器并在那里显示它。我怎样才能做到这一点?注意:psi方法是一个Promise。

app.get('/psi', function(req, res){
    res.type('text/html');
    return psi('example.com',{ key: '*Iz*Sy*****PiZ***wz7****sy*****', strategy: 'desktop'}).then( data => {
        console.log('Speed score: ' + data.ruleGroup.SPEED.score);
        console.log('done');
        res.json({ 'results' : data.ruleGroup.SPEED.score });
    });

});

1 个解决方案

#1


0  

I might be wrong but shouldn't you remove the return statement? If psi returns a promise and you return psi I believe the .then() statement should be added after the app.get function since it's returned there, but then you can't access res. So, try simply removing return?

我可能错了,但你不应该删除return语句吗?如果psi返回一个promise并返回psi,我相信应该在app.get函数之后添加.then()语句,因为它已经返回,但是你无法访问res。那么,试着简单地删除回报?

#1


0  

I might be wrong but shouldn't you remove the return statement? If psi returns a promise and you return psi I believe the .then() statement should be added after the app.get function since it's returned there, but then you can't access res. So, try simply removing return?

我可能错了,但你不应该删除return语句吗?如果psi返回一个promise并返回psi,我相信应该在app.get函数之后添加.then()语句,因为它已经返回,但是你无法访问res。那么,试着简单地删除回报?