首页 > 解决方案 > 使用 Node.js 提供动态内容并在一段时间后更新

问题描述

基本上我正在搜索一段时间后更新网站上的数据。就像,在 10 秒后更改 DOM 上的工作。

我是节点新手,所以我不知道如何制作它。

我想通过 JavaScript 向服务器发出请求,但我不确定这是正确的方法。

这是我已经制作的,但我不知道我不能重新渲染所有代码。

如您所见,我使用 express 作为我的服务器。

app.get('/proof/screen', (req, res) => {
    var selected = {};

    function instance() {

        do {
            selected = orders_[Math.floor(Math.random() * orders_.length)];
        } while(!selected || selected.financial_status !== 'paid' || !selected.shipping_address);

        res.render('proofscreen', {
            name: selected.shipping_address.first_name,
            state: selected.shipping_address.city,
            province: selected.shipping_address.province,
            country: selected.shipping_address.country,
            product: selected.line_items[Math.floor(Math.random() * selected.line_items.length)].title,
            product_link: '#'
        });

        setTimeout(instance, Math.random() * 5000);

    }

    instance();
});

所以,这段代码没有做任何事情,它只是显示错误:你不能再次发送标题。

标签: javascriptnode.jstemplatesdynamic

解决方案


推荐阅读