res.sendfile in Node Express with passing data along

前端 未结 6 1651
失恋的感觉
失恋的感觉 2020-12-01 10:35

Is there any way to redirect to an HTML file from a Node.JS application with something like: res.sendFile of express and pass a JSON data along to the html file?

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-01 11:06

    Well, it's kinda old, but I didn't see any sufficient answer, except for "why not". You DO have way to pass parameters IN static file. And that's quite easy. Consider following code on your origin (using express):

        let data = fs.readFileSync('yourPage.html', 'utf8');
        if(data)
        res.send(data.replace('param1Place','uniqueData'));
        //else - 404
    

    Now for example, just set a cookie, in yourPage.html, something like:

        
    

    And you can plainly pull content of uniqueData from yourCookieName wherever you want in your js

提交回复
热议问题