Chat Zalo Chat Messenger Phone Number Đăng nhập
What is Node? - Codecademy

What is Node? – Codecademy

JavaScript and Node.js

Javascript has been around since 1995 and has since become the dominant language for web development. For much of its life, JavaScript was primarily used for client-side scripts within <script> tags that run in web browsers. This limitation meant that developers often worked in many different languages and frameworks between the front-end (client-side) and back-end (server-side) aspects of a web application.

Although there were other projects to bring JavaScript to server-side applications, the functionality took off with the release of Node.js in 2009. Node allows developers to write JavaScript code that runs directly in a computer process instead of in a browser. Therefore, Node can be used to write server-side applications with access to the operating system, file system, and everything else needed to create fully functional applications.

Node.js is written in C, C++, and JavaScript, and is based on the open-source V8 JavaScript engine that also powers JS in browsers like Google Chrome. Because V8 supports new features in JavaScript, they are incorporated into Node.

Globals node-specific functionality

The

node provides access to several important global objects for use with Node program files. When you write a file that will run in a Node environment, these variables will be accessible at the global scope of your file.

  • module is an object that refers to the functionality to be exported from a file. In Node, each file is treated as a module.
  • require() is a function used to import modules from other Node files or packages.
  • process

  • is an object that refers to the actual computer process that runs a Node program and allows access to command-line arguments and more.

Modules

Node has many built-in modules to aid in interactions with the command line, the computer’s file system, and the Internet. These include

HTTP and HTTPS for

  • creating web servers
  • . File system, operating system, and

  • path to interact with the file system, operating system, and file/directory paths.

You can view the full documents to see more of Node’s built-in features.

Why Node?

According to .js the Node home page, Node “uses an event-based non-blocking I/O model.” In practice, this means that Node is well built to handle asynchronous JavaScript code to perform many asynchronous activities, such as reading and writing to the file system, handling connections to database servers, or handling requests as a web server.

To handle asynchronous code, Node uses a system based on callback. Node functions and methods that will implement some asynchronous activity take a callback function. This callback will be called as long as the asynchronous operation has been resolved. By convention, the first argument of this callback is an error placeholder. If the asynchronous operation failed (trying to read a nonexistent file, for example), the error argument will be an Error object, but it will be null if no error occurs.

In this example, we are using Node’s built-in fs module to read a .js script file. The callback function is called after the file read operation is completed. If an error occurs, it will be passed as an error and launched. If it does not exist, the data retrieved from the file read operation is logged in the console.

How to use

Node

This video will show you how to download and install

Node. To

get started, download and install Node.js for your operating system

.

To run JS files in Node, the node command followed by a file path will execute the program file.

For example, if we have the following saved in a file script.js:

When running the terminal command node script.js in the

same folder as script.js Node will start, I will print I am a node program in the terminal window and exit, since the script file has finished execution

. Node as REPL node It can also be used in a terminal

window

as a read-evaluate-print loop or REPL. This functionality allows you to execute JavaScript commands from the command line.

With Node installed, you can start the REPL by running the node command in a terminal and pressing Enter. You are now in an interactive JavaScript environment and can execute any valid JavaScript code, such as 4+5. After running a command, Node will always print the result of that evaluation.

In this example, the user starts the node on line one with the node terminal command. On line 2 the user types 4 + 5 and evaluates with the return key. 9 prints in the output terminal.

On line 4, the user opens a nodeIsGreat function declaration. Because this function declaration takes several lines, Node REPL will print… at the beginning of a line to show that you are still reading the user input instruction and have not yet been evaluated. After you close the function declaration on line 6, undefined is printed on the output terminal, because the function declaration itself does not evaluate to any value. When the function is invoked on line 8, Node is great! records in the console and records not defined later, because nodeIsGreat() returns undefined.

To exit the

REPL node, use the .exit command at any time and return to the system shell. Pressing ctrl + c twice will also close it.

Uploading

existing files

The REPL node can also upload existing JS files. If we had the following code saved in

script.js: We can use .load to load

it into the REPL. . load takes a path argument, so to load script.js we would use .load ./script.js.

After loading the script file, the variables can be accessed in the REPL,

so when we evaluate the variable, its value has been set by loading script.js, and

‘Node REPL is fun!’ is printed in the console.

Try it yourself by running the node in a terminal or refer to the REPL documents for more functionality. Management Node

packages

packages are a convenient way to share modules between Node developers. The npm service is the default package manager for Node and is included with a Node installation.

NPM allows access to the hundreds of thousands of open source packages available.

In addition to npm, the thread is gaining popularity as another JS package manager.

To learn more and see npm in action, take our Browser Compatibility and Transpilation course.

Node versions

Major versions of Node

aim to support the latest JavaScript features, including ES6/ES2015 and beyond without transpilation

.

For a list of JavaScript features supported in different versions, visit node.green

Version

Management

As with any major software release, top-level Node versions (8.x, 7x, and so on) sometimes introduce major changes to applications created in earlier versions of the environment. A version manager can be used to switch between multiple versions of Node on a single computer.

There are two version managers that provide this functionality: nvm (Node Version Manager) and n. N can be installed very easily as an npm package!

Contact US