Hello, I've been trying to figure this out since a while but am not able to find a solution for it in node.js
I usually code in Python and I was able to make a dynamically updating API using generators
@app.route("/api/login")
def getQR():
def generate_output():
time.sleep(3)
yield render_template_string(f"Hello 1\n")
time.sleep(10)
yield render_template_string(f"Hello 2\n")
return Response(stream_with_context(generate_output()))
The above code sends the first response "Hello 1" after 3 seconds then waits 10 seconds and adds "Hello 2" to the response. I'm trying to recreate this in Node.js but can't figure out how to do so. Here is my current code:
const server = http.createServer((req, res) => {
if (req.url == "/" || req.url == "") {
res.write("Hello 1\n");
// Some code execution for 10s
res.write("Hello 2\n")
res.end();
}
});
The above code waits for 10 seconds and then sends "Hello 1\nHello 2\n" I'm basically trying to achieve what I could do in the Python code using generators. Any workarounds/solutions or suggestions would be appreciated. Thank you!