[Node Hero] First step

Node.js in 1 chestnut

Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. Node.js uses an event-driven, non-blocking I / O model that makes it lightweight and efficient.

Here is an introduction to nodejs on its home page nodejs.org. Here, Nodejs is a runtime (run-time system: software designed to run computer programs with one or more defined languages). It was built on the basis of V8 javascript engine – the engine of Chrome's famous browser javascript code. More simply, Nodejs helps our server can be written in javascript.

The success of nodejs comes from the use of libuv library – a cross-platform support library focused on processing asynchronous I / O input / output . With the programmer, Node.js runs single-threaded , in fact the libuv library will support multithreading, file management events, event loop execution ( event loop ). etc.

Install Node.js

To install Node, go to https://nodejs.org/en/download/ find the appropriate version for your operating system and install it as normal

A little more advanced, you can install nodejs via the library: https://github.com/creationix/nvm . This library helps you install and manage multiple nodejs versions on the same machine. For window users you can use nvm-windows: https://github.com/coreybutler/nvm-windows

If installing nodejs via nvm, you can easily interact via CLI. Since the organization that manages Nodejs was established in the year outside with the participation of Linux Foundation, nodejs began to have a clear launch plan. In that plan you will see there are two versions of nodejs: LTS and current. The LTS version is a highly stable version, with little update on new features es6, focusing only on fixing bugs and optimizing the system. Currently LTS version stops in version 4. Current version or experimental version is always updated with new features – the speed of upgrading is always ?

Install via nvm

To install nodejs you use syntax

To check the node version in the machine you can use one of two syntaxes

If you see the information displayed on the nodejs version is good: D.

To switch between nodejs versions installed on your computer, use the syntax

Hello World

Let's start nodejs with the terminal. Type node

Here you can code and run that javascript:

Type enter and

This is the screen print command hello form Nodejs.js . This command does not return anything, so you get undefined soon after. Negative yet: D. Now start creating the first application. Please open any code editor Atom, Sublime or Visual Code. Create a file called index.js with the following content

To run the last file, type node <link to that file> . You will get the same result as above when interacting with the terminal

Module application

Now that you have the index.js file , make it a bit more complicated. Usually when our code often breaks our code into multiple files with easy-to-read and easy-to-maintain functionality. Let's go back to the editor and create the following directory and file structure:

Note here that you do not need to create a package.json file, this file can be created automatically by the command. Each nodejs application contains a package.json file that contains information about the application and dependent libraries. We will learn more about NPM in the next post.

To create the package.json file , go to the terminal and move to the application folder and type npm init . Then answer the questions so that the system builds itself into the package.json file . After you have answered, you will receive the file with the same structure as follows:

Here you add the start command in the scripts section to run the application.

With a number of Paas systems – simply understand the hosting server support system, it will find the start command in the package.json file to start your application.

Complete the application only. This app will sum the numbers in a given array placed in the app / index.js file . First open the index.js file outside the app directory and copy the following command into it.

With the index.js file in the app directory you add the array data you want to add

Here we still lack the logic to calculate the total. Copy the following code and place it in app / calc.js

Ok you have completed the first application, run it. In terminal, type npm start , you will get the result The result is: 19 . You can change the array data in the app / index.js file to get another result: D.

ITZone via kipalog

Share the news now