Introduction to Node.js

Node.js is an open-source JavaScript runtime environment. Node.js is a cross-platform runtime environment so developers can use it in almost any environment such as Windows, MacOS & Linux.

Node.js uses the V8 JavaScript engine, which browsers like Google Chrome & FireFox uses to run JavaScript in browser. With Node.js, this V8 engine now works outside of the browser. This means now you can run JavaScript directly on your computer or server.

When you write JavaScript code, Node.js does the following. Let’s break down step-by-step:

  1. Environment Creation: Node.Js will setup an environment where your JavaScript code can run outside of the browser.
  2. V8 Engine’s Role: The V8 engine now takes your JavaScript code and convert it into machine readable code.
  3. Execution: Node.js uses the same machine code created by the V8 engine to execute outside of the browser.

But here’s one thing to understand. Node.js does not automatically display output in the browser. For example:

  • If you write console.log(“Hello, Node.js!”);, it shows the output in the terminal.
  • If you want to display something in a browser, you need to create a web server using Node.js (e.g., with the http module) and send data to the browser manually.

Example of Displaying Output in the Browser:

Here’s how you could make Node.js send a response to the browser:

<code>const http = require("http");

// Create a server
const server = http.createServer((req, res) => {
  res.writeHead(200, { "Content-Type": "text/html" });
  res.write("<h1>Hello, Browser!</h1>");
  res.end();
});

// Start the server
server.listen(3000, () => {
  console.log("Server is running on http://localhost:3000");
});

</code>
JavaScript
  1. Run this file with node filename.js.
  2. Visit http://localhost:3000 in your browser.
  3. You’ll see “Hello, Browser!” displayed.

In summary:

  • Node.js runs JavaScript code outside the browser.
  • It doesn’t directly display anything in the browser unless you create a server.

Difference between Node.js and Browser?

JavaScript is the only programming language used by both Node.js and the browser. One should always be clear that building apps that run in a browser is completely different from building a Node.js application. They are completely different.

If you are a frontend developer who uses JavaScript for frontend development. Now with Node.js, you can use JavaScript for building your backend and it is a huge advantage.

  • Environment Differences:
    • In the browser, you work with the DOM and Web APIs (e.g., document, window), which don’t exist in Node.js.
    • In Node.js, you get powerful modules like filesystem access, which aren’t available in the browser.
  • Environment Control:
    • With Node.js, you control the runtime version, allowing you to use modern JavaScript features without worrying about browser compatibility.
    • Browsers may require tools like Babel to ensure older JavaScript versions work.
  • Module Systems:
    • Node.js supports both require() (CommonJS) and import (ES Modules).
    • Browsers primarily use import as they adopt ES Modules.

In short, while both environments use JavaScript, their tools, APIs, and ecosystems differ significantly.

The V8 Javascript Engine

V8 is the JavaScript engine that powers Google Chrome and Node.js. It parses and executes JavaScript code. The V8 engine is independent of the browser which enabled the rise of Node.js in 2009, allowing it to run server-side JavaScript.

Other browsers use different engines:

  • Firefox: SpiderMonkey
  • Safari: JavaScriptCore (Nitro)
  • Edge: Now uses V8 (formerly Chakra).

All those engines implement the ECMA ES-262 standard, also called ECMAScript, the standard used by JavaScript.

V8 is written in C++, and it’s continuously improved. It is portable and runs on Mac, Windows, Linux, and several other systems.

Modern JavaScript engines, including V8, use just-in-time (JIT) compilation for better performance, transforming JavaScript from a simple scripting language into a robust platform for large-scale applications.

This ongoing evolution ensures faster, more optimized performance for both web and server-side environments.

You may also like