Node.js (Node)

What is Node.js (Node)?

Node.js (Node) is an open source, cross-platform runtime environment for executing JavaScript code. Node is used extensively for server-side programming, making it possible for developers to use JavaScript for client-side and server-side code without needing to learn an additional language. Node is sometimes referred to as a programming language or software development framework, but neither is true; it is strictly a JavaScript runtime.

Node incorporates the V8 JavaScript engine, the same one used in Google Chrome and other browsers. It is written in C++ and can run on macOS, Linux, Windows and other systems. The engine parses and executes JavaScript code. It can operate independently of a browser environment, either embedded in a C++ application or implemented as a standalone program. The V8 engine compiles JavaScript internally, using just-in-time (JIT) processes to speed up execution.

The following code is an example of a simple JavaScript file (server_tst.js) written for the Node environment. The script starts by loading the Node.js Hypertext Transfer Protocol (HTTP) module. The module includes a variety of classes and methods for implementing an HTTP server.

Node.js example


Example of a simple JavaScript file written for the Node (Node.js) environment.

After Node.js is installed on a computer, the file can be run using a simple command: node server_tst.js. The JavaScript code instructs Node to carry out two basic operations:

  • Display a message in a browser on the local machine when connecting to http://localhost:2000. The message reads, “This is a test of Node.js on a local computer.”
  • Return a message to the console when the command is run. The message states, “Node.js server running at”

More details about the components in this script and the HTTP module in general can be found by referring to the HTTP topic in the official API reference documentation published by the Node.js organization.

How does Node.js work?

A Node application runs in a single process. Node does not create a new thread for every request, as is often the case with traditional server-side programs. In this way, a Node server can handle thousands of concurrent connections without having to contend with thread concurrency issues or the overhead multithreading brings.

Node.js is event-driven and runs asynchronously. Code written for the Node environment does not follow the traditional model of receive, process, send, wait and receive found in other systems. Instead, Node implements an event loop that processes incoming requests as they stack up in the event queue, handling small requests one after the other without waiting for responses.

This is a shift away from mainstream models that run larger, more complex operations and process several threads concurrently, with each thread waiting for its appropriate response before moving on.

The Node.js approach offers a major advantage over these models, according to its creator Ryan Dahl. Node does not block input/output (I/O) operations like more traditional approaches. This is in large part because Node functions do not perform I/O directly, which

Shubham Patil

Typically replies within a day

Powered by WpChatPlugins

Learn to code by doing. Try hands-on Coding with RLchats PRO.