Node.Js with Livia || Node. Js In a Nutshell

Node.Js with Livia || Node. Js In a Nutshell

Hello ☺️ I am back and of course, Happy New Year, if you stumbled on my blog today you should be grateful that you made it to 2023. It’s a new year and I decided to do something different, drum rolls please 🥁 🥁

So I would be starting a Nodejs Series which would have 12 articles that is one article for each month of 2023, just like a monthly newsletter on node.js 😂 I started Nodejs in August 2022 and I felt I needed to share the little I know.

In this article we would be looking at an overview of node.Js, the V8 Engine and we would run a node.Js application.

Let’s learn Node.js together🥰

What's Node.Js?

Node.js is an open-source server that runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.). Node.js is a backend JavaScript runtime environment that runs on the V8 JavaScript Engine and executes JavaScript code outside a web browser.

In simple terms, the V8 JS engine from chrome was extracted and using it a new technology has been made to run standing alone.

V8 JavaScript Engine 🤔

V8 is the JavaScript engine (i.e. it parses and executes JavaScript code) that powers Google Chrome. It's the thing that takes our JavaScript and executes it while browsing with Chrome.

V8 JavaScript engine was initially developed for Google Chrome and Chromium web browsers to improve the performance of JavaScript execution. The engine was initially designed solely for execution by web browsers, but the latest versions also execute JS code outside the browser, enabling server-side scripting.

To achieve faster JavaScript execution speeds, V8 translates JS code to more efficient machine code instead of using an interpreter. Most modern JavaScript engines like SpiderMonkey or Rhino follow the same approach, but what makes V8 stand out is that it does not produce any intermediate code.

Node.Js Vs Browser side Js

Node.Js and Browser Side Js can be distinguished in terms of Application, System access, Objects and Running engines.

  • Application: Browser.js is mainly used for client-side applications, as the name suggests it gets executed in the browser only, while in the case of Node.Js javascript code gets executed outside the browser used for server-side applications.

  • System Access: Node.Js has full system access i.e can read and write directly to the file system, while Browser.Js have access limited to the browser.

  • Objects: Node.Js has two objects global object (contains several functions that are not available in browsers as they are needed for server-side works only) and Require object (which is used to include modules in the app). On the other hand browser Js have window, location and document objects.

  • Running Engine: Browser.Js runs on any engine depending on the browser for instance on Firefox it runs on Spider monkey engine and for Safari it runs on JavaScript Core, while Node.js runs in a V8 engine which is mainly used by google chrome.

Getting Started

Let's get you started. You would be installing Node.Js and writing your first node.js application ☺️ so get your laptop !!!

Installing Node.Js

Like python, you have to install Node.Js for it to work properly on your machine.

Node.Js can be installed in so many ways, It can be installed by downloading the package installer (there are different package installers for different environment). You can install Node by grabbing a copy of the source code and compiling the application. Another way of installing Node is by cloning the GIT repository in all the three environments and then installing the one peculiar to your environment.

Writing a node.Js Web Server

  • Create a file server.js
const http = require('http');
const port = 3000;

const server = http.createServer( (req,res) =>{
    res.writeHead(200);
    res.end("My first server!");
}

server.listen (port, () => {
   console.log(`Server running on port ${port}`);
});

Explaining the above code:

  • Before you can create a web server the Node.Js http module is imported with the line

    const http = require('http');

    The http module contains the function to create the server.

  • Declare the port which the server will be running on

    const port = 3000;

  • The createServer() method of http creates a new HTTP server and returns it.

    const server = http.createServer();

    • The first line res.writeHead(200); sets the HTTP status code of the response. HTTP status codes indicate how well an HTTP request was handled by the server. In this case, the status code 200 corresponds to "OK".

    • The next line of the function, res.end("My first server!");, writes the HTTP response back to the client that requested it. This function returns any data the server has to return. In this case, it’s returning text data.

  • The server is set to listen on the specified port which has been declared. When the server is ready, the callback function is called, in this case informing us that the server is running.

    server.listen(port, () => { console.log(`Server running on port ${port}/`);

    });

With less than twenty lines of code, we now have a web server. Let’s see it in action and test it end-to-end by running this program in the terminal:

node server.js

In the terminal we will see this output:

Server running on port 3000

To view the response on your web browser;

  • Open your web browser

  • While the server is still running, type this url:

    http://localhost:3000/

  • The final result displayed on the browser:

      My first server!
    

Node.Js is definitely an interesting server-side scripting tool. During the course of writing this article I could not figure out if Node.Js was also a JavaScript framework. I saw a lot of things on the internet, I would love to read your thoughts on whether Node.Js is a framework or not please drop your thoughts in the comment section.

Thanks for reading, see you next month 🥰