Tuesday, 14 March 2017

NodeJS non-blocking I/O nature

I read about non-blocking nature of nodeJS, and how I/O operations are non-blocking. I created a simple test to prove this

var request = require('request');
var http = require('http');
var express = require('express');

var app = express();

app.get('/test1', function (req, res) {
  res.sendStatus(httpStatus.OK);
});
app.get('/test2', function (req, res) {
  request.get('https://httpbin.org/delay/15', function () {
    res.sendStatus(httpStatus.OK);
  });
});
var server = http.createServer(app);
server.listen(3003);

module.exports = app;

That's the whole test. test1 endpoint returns OK immediately, while test2 returns OK after 15 seconds due to http request that is sent. When I call test2 and immediately after that call test1, the response for test1 is returned after 15 seconds. I would expect if I/O operations are non-blocking that response for test1 is going to be returned immediately.

What am I missing?



via lolotron

No comments:

Post a Comment