HTTP2 Express推送

时间:2018-07-05 14:39:49

标签: node.js express http2 spdy

我正在尝试为自己构建的Express应用设置HTTP2。据我了解,Express不支持NPM http2模块,因此我正在使用SPDY。这就是我正在考虑的方式-感谢执行了类似操作的人员的建议。

1)服务器设置-我想用SPDY包装我的现有应用程序,以保留现有路由。选项只是带有密钥和SSL证书的对象。

const app = express();
...all existing Express stuff, followed by:

spdy
    .createServer(options, app)
    .listen(CONFIG.port, (error) => {
    if (error) {
        console.error(error);
        return process.exit(1)
    } else {
        console.log('Listening on port: ' + port + '.')
    }
});

2)此时,我想通过条件PUSH响应来增强一些现有路由。我想检查是否有向客户端发出路由请求的更新(客户端称为终结点,而更新是称为终结点更改的JSON对象数组),如果是,则推送至客户端。

我的想法是,我将编写一个将res作为其参数之一的函数,将端点更改另存为文件(我尚未找到推送非文件数据的方法),然后将它们添加到推送流,然后删除文件。这是正确的方法吗?我还注意到流有第二个参数,它是一个req / res对象,我在这里正确格式化了吗?

const checkUpdates = async (obj, res) => {
    if(res.push){
        const endpointChanges = await updateEndpoint(obj).endpointChanges;
        if (endpointChanges) {
            const changePath = `../../cache/endpoint-updates${new Date().toISOString()}.json`;
            const savedChanges = await jsonfile(changePath, endpointChanges);
            if (savedChanges) {
                let stream = res.push(changePath, {req: {'accept': '**/*'}, res: {'content-type': 'application/json'}});

                stream.on('error', function (err) {
                    console.log(err);
                });

                stream.end();
                res.end();
                fs.unlinkSync(changePath);
            }
        }
    }
};

3)然后,在我的路线内,我想使用相关参数调用checkUpdates方法,如下所示:

router.get('/somePath', async (req, res) => {
    await checkUpdates({someInfo}, res);
    ReS(res, {
            message: 'keepalive succeeded'
        }, 200);
    }
);

这是实现HTTP2的正确方法吗?

0 个答案:

没有答案
相关问题