Tuesday, 27 September 2016

Introduction to Nodejs

Definition: 
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-drivennon-blocking I/O model that makes it lightweight and efficient.

Nodejs is developed by Ryan DahlYou can see detailed description of Nodejs video on YouTube , Introduction to Node.js by RyanDahl where he exactly shows how node can be a lot faster than previous synchronous events.



As an asynchronous event driven JavaScript runtime, Node is designed to build scalable network applications. 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 = 3000;

const server = http.createServer((req, res) => {
  res.statusCode = 200;
  res.setHeader('Content-Type', 'text/plain');
  res.end('Hello World\n');
});

server.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, since there are no locks. Almost no function in Node directly performs I/O, so the process never blocks. Because nothing blocks, scalable systems are very reasonable to develop in Node.


now open your browser and go to url mentioned in the above program.
i.e. http://127.0.0.1:300
and you wil see 'Hello World'.

Congreates !! you have successfully made an http connection .
Now Lets move on with node setup , in case you are perfect beginner .

1 comment:

  1. Good post and informative. Thank you very much for sharing this good article, it was so good to read and useful to improve my knowledge.
    Node JS Online training Node JS training in Hyderabad

    ReplyDelete