简体   繁体   中英

How to return the data received from PageSpeed Insight from node in a route?

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.

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

});

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 ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM