What Is Node.js? Here’s How to Use Server-side JavaScript

With Node.js, you can add server-side functionalities to your applications using JavaScript (JS).

Before the introduction of Node.js in 2009, JavaScript was recognized as a frontend programming language, which meant that it was only used to manage aspects of a web application visible to the user.

Node.js is a game-changer. It allows developers to use JavaScript as a server-side language, effectively transforming JavaScript from frontend to full-stack.

What Is Node.js?

It’s important to understand that Node.js isn’t a programming language, but a run time environment of a programming language. Node.js is a server-side, packaged software that contains predefined processes to accomplish specific tasks.

As a server-side runtime, every Node.js process is executed on a server; essentially working on the backend aspect of an application to manage data. For instance, if you wanted to store some data in a file or a database, you’d need to employ the use of a server-side language or application.

Node.js is labeled as a JavaScript run-time environment because it uses JavaScript to conduct backend processes.

What Makes Node.js Special?

If you’re familiar with JavaScript you should know that it’s a client-side language, so it makes it possible for you to click a button and submit some information contained in a form. However, that’s as far as it goes; for that information to be stored in a file or a database, some other language would generally have to take over.

Node.js is so special because it gives developers the tools needed to connect to a file or database and store the data that was initially submitted from that form.

Before Node.js, a developer would need to know JavaScript along with other backend programming languages—such as Java or Python—to be called a full-stack developer. Today a full-stack developer can choose to learn only JavaScript and still be able to develop complete websites and applications.

How Does Node.js Work?

Node.js is built on the V8 JavaScript engine, which is used to compile and execute JavaScript source code. So when you execute a JS script using Node.js, that code is initially passed to the V8 JavaScript engine. The V8 JavaScript engine then compiles the script and passes the result of the compilation back to Node.js where it can be used in the application.

Why Use Node.js?

Node.js is a pretty popular backend technology used by big companies likes Netflix and Uber. There’s no doubt that Node.js developers are in demand. So why is this technology so popular?

Related: How to Install and Manage Multiple Versions of Node.js on Linux

Node.js employs a non-blocking I/O module, where I/O stands for input and output. This critical feature is one of the reasons for the technology’s popularity. Node.js being non-blocking means that while an I/O operation is being executed, access is still granted to other aspects of the application currently carrying out this I/O operation.

For context, consider the example of using a database with a web application. If a user wanted to retrieve extensive data from this database (a process that’s going to take some time) every other feature on this application (like clicking a random button) would be disabled until the I/O operation is completed if Node.js wasn’t using a non-blocking I/O module.

Creating a Node.js Script

A fundamental feature of Node.js is its node module system. This is a collection of different Node.js application programming interfaces that can be used to accomplish any task, from printing data to a console to storing data in a file.

One of the most popular Node.js modules is the file system module. It allows the developer to create and communicate with files on any given machine.

Using the File System Module Example

const fs = require('fs');

fs.writeFile('tasks.txt', 'buy groceries', (error) =>{
if (error) throw error;
console.log('The file has been saved.')

To use the file system module in your Node.js projects, you’ll first need to import this module. In Node.js, the file system module is represented by the acronym fs. So by simply passing fs to the required function (as shown in the code above), you now have access to the file system module.

The file system module is passed to the variable called fs, which could be whatever name you think is appropriate. That name was chosen because it accurately represents what will be stored in the fs variable.

The file system module has an extensive list of functions; the one used in the code above is called writeFile. The writeFile function takes three arguments: a filename, the data that is to be stored in the file, and a callback function.

The callback function takes an error argument that’s only available if a problem arises when trying to execute the writeFile function.

Executing a Node.js Script

To execute a Node.js script, all you need to know is the name of the file that this script is stored in. Node.js is written in JavaScript; therefore, every Node.js code has to be stored in a JavaScript file to be executable.

The code above is stored in a file called index.js. So to execute the file above (assuming that Node.js is already installed on your machine) you’ll need to launch a terminal/console and cd into the direct folder that contains the index.js file. After you have gained direct access to the index.js file, you simply type the following line of code in your console.

node index.js

Executing the line of code above will produce the following result in the console.

The file has been saved.

This means that a new text file called tasks that contains the text “buy groceries”, has been successfully created and can be found on your machine in the folder that contains the index.js file.

Now You Can Perform Server-Side Operations in JavaScript

One major take away from this article is how Node.js has revolutionized the use of JavaScript; because of Node.js, JavaScript developers can now be recognized as backend, or even full stack developers.

As a JavaScript developer, you can now create and execute server-side processes (such as creating a file and storing data to it), using Node.js.

Now all that’s left for you to do is to decide whether you want to be a backend, frontend, or full stack JavaScript developer.

Source link


Leave a Reply

Your email address will not be published. Required fields are marked *