Yuliya Veklicheva
Yuliya Veklicheva

Reputation: 21

Is nodeJs event-driven?

In "Professional NodeJS" I found this sentence "This project (NodeJS) was not like other server-side JavaScript platforms where all the I/O primitives were event-driven and there was no way around it." But, as I know, NodeJS is event-driven and streams in NodeJS are event-driven. So can anybody explain this sentence?

Upvotes: 2

Views: 147

Answers (1)

sam100rav
sam100rav

Reputation: 3783

Node.js is an asynchronous event driven framework. In the following "hello world" example, many connections can be handled concurrently. Upon each connection the callback is fired, but if there is no work to be done Node is sleeping.

const http = require('http');

const hostname = '127.0.0.1';
const port = 1337;

http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello World\n');
}).listen(port, hostname, () => {
  console.log(`Server running at http://${hostname}:${port}/`);
});

This is in contrast to today's more common concurrency model where OS threads are employed. Thread-based networking is relatively inefficient and very difficult to use. Furthermore, users of Node are free from worries of dead-locking the process—there are no locks. Almost no function in Node directly performs I/O, so the process never blocks.

For detail go to source.

Upvotes: 2

Related Questions