Reputation: 15
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!
Upvotes: 1
Views: 1105
Reputation: 16544
According to python doc, generator is for data streaming:
Sometimes you want to send an enormous amount of data to the client, much more than you want to keep in memory. When you are generating the data on the fly though, how do you send that back to the client without the roundtrip to the filesystem?
The answer is by using generators and direct responses.
Check these references:
Understanding that what you need is data streaming to your browser for big files transfer, not the classic res.send("hello")
, basically the flow is:
Here an example:
import {createReadStream} from 'fs';
import express from 'express';
const app = express();
app.get('/', (req, res) => {
var readStream = createReadStream('./data.txt');
readStream.on('data', (data) => {
res.write(data);
});
readStream.on('end', (data) => {
res.status(200).send();
});
});
app.listen(3000);
Check these references:
Upvotes: 1
Reputation: 1095
We will create a time.sleep()
alternative in js. First create a function that would call setTimeout.
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
Use await
to call this function. Learn more about await here.
await sleep(3000) // waits for 3 seconds
Upvotes: 0