Nodejs 0 Basic Detailed Tutorial 1: Installation + basic Concepts

Source: Internet
Author: User
Tags install node

Chapter I recommended 2-hour course of study total 10 chapters

How to learn: read it in detail and implement the relevant code manually

Learning Goals : This tutorial will teach you to install node, build servers, Express, MySQL, MongoDB, write background business logic, write interfaces, and finally complete a complete project backstage, a total of 10 days is expected.

Daily update, suggest with learn, follow this tutorial study must be able to learn Nodejs, Midway Discovery Tutorial wrong place, please timely message feedback

node. JS Installation

Download via official website

node. JS Chinese web http://nodejs.cn/download/

Windows systems we generally choose the MIS version, according to their own computer system and the number of bits selected

Mac Select. Pkg version

Specific version number, be sure to install even-numbered version, because the base version for the development version, even version of the stable version

After downloading the installation package directly double-click Run, the next step, the recommended choice to install to the default directory, install to another directory may need to reconfigure environment variables (more cumbersome)

There will be a consent option in the installation process, which can be checked before the next step

Installation complete--After clicking Finish, there will be no interface prompt or desktop icon

Verify that the completed method is installed:

Click on the Computer Start menu, search cmd, click Open cmd command line, such as:

Then enter NODE-V Click on the ENTER key, as shown in the version promotion, the table installation is complete

Node Introduction

node. JS is a JAVASCRIPT runtime environment based on the Chrome V8 engine. node. JS is a platform built on the chrome JavaScript runtime to easily build responsive, easy-to-scale Web applications. node. js is lightweight and efficient with event-driven, non-blocking I/O models, making it ideal for running data-intensive real-time applications on distributed devices.

In fact, node. JS is an environment where JavaScript can be run on the server-the operating environment

Now there are many companies using node as the server language, foreign: linkedin/paypal/twitter domestic: Know the main station push, NetEase (part of the backstage), Ali (part of the backstage)

Also for small and medium-sized projects in the background of rapid development is very suitable.

blocking and non-blocking explanations : Here we have to deal with the concept of synchronous and asynchronous,

Synchronization: Our JS code is executed from the top down, the previous code does not complete, the next code is not executed is synchronous.

Async: The code below can be executed at the same time as the code above executes.

Then blocking is the state representation of the synchronization, not blocking is the asynchronous state representation.

Start the program

Helloword Example:

command Line Input node press ENTER to confirm that you can see the following side of the prompt ">_" indicates that the node has entered the running state, you can enter the JS code (and the previous page is written in JS basically consistent)

Here we enter Console.log ("HelloWorld") to make sure you can see the printed HelloWorld

But what is the undefined on the way to the second horizontal line? Here in node, each line of code executes, the return value is displayed, there is no return value, so there is a undefined

In addition, let's try the following code, define the a=4,b=5, and output the result after the addition

If you want to end node's running state, press CTRL + C two times to exit and execute the status as.

Run the JS file:

Recommended to create a folder to put JS, named NodeTest

Then create a JS file inside, named Main.js, (note that the file encoding format is best utf-8, otherwise the output Chinese will be garbled.) )

Edit Main.js. Write such as code, output folder name and file name, __dirname and __filename as node internal variable, indicating folder and file name. (Note: Two underscores are attached)

// output a line of text // output folder name // Output File name

How does the JS file that is written work with node?

In front of our command line is opened from the Start menu, node's execution is under the User Administrator folder, see

If we are going to execute node in a custom folder, we can navigate to the directory by holding down SHIFT and right-clicking in our own folder and clicking "Open command line Here" in the menu.

The command line that opens now navigates to the current folder.

At this point, directly entering the file name of node needs to be executed, you can see the printed three rows of data HelloWorld and the folder name and file name

Implementation of asynchronous

There are two ways of implementing Asynchrony: 1, callback function 2, timer

callback function: after listening to an event activity, then execute other code, this situation does not affect the subsequent code execution, is asynchronous, the specific example later.

timers:setTimeout and SetInterval

All asynchronous manipulation, in JS internal will be the first to execute synchronous code, for the asynchronous code will be put to the last execution

Try the following example: Create a main2.js in the previous folder to write the following code, and then run, we will see that the for loop execution is finished before the settimeout is printed, even if the out time is set to 0 seconds.

SetTimeout (function() {    console.log (1000000000);},0);  for (var i=0; i<1000; i++) {    console.log (i);}

OK, so much today, tomorrow will explain: Modular, FS file operation module, HTTP creation service module.

Nodejs 0 Basic Detailed Tutorial 1: Installation + basic Concepts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.