温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.muo.com/node-js-server-side-javascript/
点击访问原文链接

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

What Is Node.js? Here's How to Use Server-side JavaScript Menu Sign in now Close PC & Mobile Submenu Windows2 Linux Android Apple Technical Submenu Tech Explained Security Software Submenu Productivity Internet Creative Screen Submenu Entertainment Streaming Home Submenu Smart Home Home News Sign in Newsletter Menu Follow Followed Like More Action Sign in now Productivity Android Smart TVs Networking Windows 11 Entertainment Close
What Is Node.js? Here's How to Use Server-side JavaScript By  Kadeisha Kean Published May 4, 2021, 1:00 PM EDT Kadeisha is a Full-Stack Software Developer and Technical/Technology Writer. She has a Bachelor of Science in Computing, from the University of Technology in Jamaica. Sign in to your MakeUseOf account

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
// Import the file system module
const fs = require('fs');

// Create a new text file and store a string in it
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.

Close
Recommended Front-End vs. Back-End Web Development: Which Path Is Right for You? Your RAM might be running in single-channel mode — here's how to check in 10 seconds I bought a Steam Deck without spending a fortune, and you can too I repurposed 4 old Apple devices, and now they all do something useful in my home Join Our Team Our Audience About Us Press & Events Media Coverage Contact Us Follow Us Advertising Careers Terms Privacy Policies MakeUseOf is part of the Valnet Publishing Group Copyright © 2026 Valnet Inc.

What Is Node.js? Here's How to Use Server-side JavaScript,AI智能索引,全网链接索引,智能导航,网页索引

    Upgrade your programming skillset from front-end to full stack with Node.js