将外部数据传递到服务器发送事件的快速发送(res)功能

问题描述 投票:0回答:0

我只熟悉将参数传递给函数 ('s parameters) 的概念。 我不熟悉通过参数 (res) 发送响应的函数的明显“反向”概念。

我的目标是将数组传递到发送函数和服务器以通过已建立的流发送事件。

它与发送函数内部的代码配合得很好,但我不知道如何从外部模块注入它。

导出发送并在外部模块中要求它抛出:类型错误:发送不是函数。 它似乎是 express 的一个组成部分,而不是一个函数。 因此我无法试验 argument.length 循环等

任何帮助将不胜感激。

app.get('/sse-stream', (_req, res, next) => {
    try {
        res.setHeader("Content-Type", "text/event-stream");
        send(res);
    } catch (err) {
        next(err); 
    }
});


function send(res) {

        // Inject setJson here from custom module
        // exporting send and requiring it in external modul throws: TypeError: send is not a function

        let setJson = JSON.stringify(input);
        res.write("data: " + `${setJson}\n\n`);
    }
node.js function express server-sent-events
© www.soinside.com 2019 - 2024. All rights reserved.