I have an Express URL which has to wait for data to arrive from an external device over a serial port (or another network connection). This can take up to two seconds. I understand that if my get function blocks, it blocks the entire Node process, so I want to avoid this:
app.get('/ext-data', function(req, res){
var data = wait_for_external_data();
res.send(data);
});
I do have an emitter for the external data, so I can get a callback when external data arrive.
I'm unclear on how to tell express to do other things while my code is waiting for external data to become available, and how to pass them on to the repose object once I have them.
Generally you would pass a callback to your wait_for_external_data function that will be called once the data is received, and you need to write wait_for_external_data such that it will not block. To do this you would use the event emitters to get the data, as you mentioned. I can give more info if you elaborate on what library you are using to get the data.
app.get('/ext-data', function(req, res){
wait_for_external_data(function(data){
res.send(data);
});
});