/ Getting Started

Understanding the Basics of Node JS

Node JS is a JavaScript runtime environment, easy to think of as JavaScript on the server. Node js uses V8 engine and includes some additional modules that give you more browser features and JavaScript functions.

The role of node js in web development is …

  1. To run server which is creating a server and listening to incoming requests.
  2. To handle business logic. For instance, handle requests, validate input and connect to database.
  3. To return responses such as HTML, JSON, XML or files to client.

How to create a node.js server

  1. Import http module: const http = require('http');
  2. Create a server: const server = http.createServer((req, res) => { });
  3. Execute and listen to the server: server.listen(3000);
const http = require('http');

const server = http.createServer((req, res) => {
    console.log(req);
});

server.listen(3000);
  1. To make a request to the server, open a browser and navigate to http://localhost:3000. You will see the req in the console!
  • To start the Node.js server: node fileName ex) node index.js
  • To quit the running Node.js server: CTRL + C

Requests and Responses

: Below code is a very basic example to handle requests and responses for understanding the concepts.

const server = http.createServer((req, res) => {

    // To see requests, 
    console.log("URL: "+req.url)
    console.log("METHOD: "+req.method)
    console.log(req.headers);
    
    // To send responses, 
    res.write(`
        <html>
            <body>Hi from NODE JS server!</body>
        </html>`
    );
    res.end();
});

How Node JS works

- Asynchronous and Event Loop

Node JS is non-blocking code or asynchronous architecture by default, which a single thread is handling multiple requests. In contrast, Other frameworks or languages such as ASP.NET, Flask and Ruby are blocking code or synchronous architecture, which one thread will handle only one request at a time.

Let’s say there are request A and request B to be handled in our list in order. First, our Node JS thread will serve the request A. If request A needs to query a database, the thread doesn’t have to wait for the database to return the data. While the database is executing the query, the thread will serve the next one - request B.

When the database is ready to return the result for request A, it pushes a message in Event Queue. Node monitors Event Queue continuously in the background. When an event is found in Event Queue, the thread will take the event out and process it. Then Node will check if Event Queue is empty constantly to handle next Event Callbacks. This loop process is called Event Loop including Timers, Pending Callbacks, Poll, Check, Close Callbacks.

Node JS has an ongoing Event Loop as long as there are listeners. If you create a server, it always creates a listener. Therefore, Node JS server will never stop unless you unregister by using process.exit.

Because of these features, node appications are ...

  • Faster response time
  • Agile development
  • Data-intensive apps
  • Real-time apps