How do I learn nodejs-Note 1

Source: Internet
Author: User
Tags node server

In fact, at the beginning, I had some knowledge about nodejs.

For example, I know that it is a server-side web server in the Javascript language. For example, I know that its advantage lies in its performance, and the reason for its outstanding performance lies in the efficient V8 engine, that is, the compiler and other language-level stuff are nothing more than the characteristics of some JS languages, functions, Asynchronization, callback and so on.

However, in actual learning, I still encountered many difficulties that I couldn't imagine at the beginning. Below is a simple record of the learning process.

1.

Of course, Google started.

The first search record is obviously an official website www.nodejs.org, which is very concise.

Find a large Download button in the middle. If it is found that it is an MSI file, consider what else and install it. (Windows OS)

The installation process is simple.

Later, I found that both Linux and Mac are more difficult than windows, and the young people in windows are really white. (Currently, it is only in Windows)

2.

OK, start the programmer's first step "Hello World" forever ".

VaR HTTP = require ("HTTP ");

HTTP. createserver (function (request, response ){

Response. writehead (200, {"Content-Type": "text/plain "});

Response. Write ("Hello World ");

Response. End ();

}). Listen (8080 );

In this case, copy these sentences to a server. in the JS file, and then you need to open the command line (powershell is strongly recommended, I do not understand how to stop or restart the window with cmd), run

Node server. js

Next, open your browser and access http: // localhost: 8080/. You will see a webpage with the name "Hello World.

I should be able to understand these words, but I will not explain them.

3.

Next, I naturally want to get the server of the web socket. So I continued to Google and found that the socket module of nodejs needs to be installed. io (my personal understanding is similar to the meaning of a third-party library in Java)

The method for installing the module is generally through NPM. There is no concept for NPM, So Google continues.

Find an article about NPM at http://howtonode.org/introduction-to-npm, and I will find some explanations. (Good article, recommended)

NPM is the abbreviation of nodejs package management. Currently, nodejs has NPM.

Here we will briefly introduce several NPM commands,

NPM list-view the currently installed modules

NPM version-view the current NPM version

NPM install-install a new module

NPM Update-update Module

NPM help-help (this is the most important)

Other commands can be used to query the Help file or hit the top npm help command

The installation command is very simple. You can run NPM help install to get more help (it is found that the NPM help file is quite good, for example, running the above command will automatically open an HTM Help File)

NPM install socket. Io

NPM install Express

Some articles will prompt that the command can be added with-G or-D (if you do not try-G, you will find that the express command is not easy to use, the reason is not clear), for example

NPM install-G socket. Io

Search for-G and-D, and find the answer in the config help file.

According to the Help file,-G and-D are actually shorthand. To put it bluntly, they represent the escape characters.

G-Global

D-log lever info

There are other escape characters that you can find in the Help file.

The effect of the preceding escape character is as follows:

If-G is added, the nodejs module is installed in the path C: \ Users \ User Name \ appdata \ roaming \ NPM \ node_modules. If no node is added, it will be installed under the node_modules path of your nodejs installation path.

Note: there is an important difference here. If you select-G or global installation, you can directly use commands such as express and jade in the command line, the reason is that when nodejs is installed, the path c: \ Users \ User Name \ appdata \ roaming \ NPM \ node_modules is automatically added to the environment variable of the system.

If you do not want to add-G, if you want to use commands such as express and Jade, you need to find the. bin directory where the express command is located in node_modules directory of nodejs.

If-D is added, all installation logs are displayed.

 

4.

So I started to install the module. It seems very simple, but this problem occurs. Why can't I connect to the server?

This error is always prompted.

Npm http get https://registry.npmjs.org/express

Error: failed to fetch from Registry

After reading the command and execution process, I suspect that some packages and files have been downloaded.

I am tossing in the unit, so I suspect it is a problem with the network environment, because there is no way to pass the server review, so the download is not allowed.

(1) The first thought was to change HTTPS to HTTP, instead of using the HTTPS transmission protocol such as SSL, so I found the command to modify the configuration.

NPM config set registry http://registry.npmjs.org/express

NPM config set strict-SSL false

Invalid after modification

(2) because it is actually downloading files, all the websites mentioned that the default token can be

(3) After changing the network environment, I found that I could install the package. I felt like I had downloaded the installation package and there would be no other changes (such as writing to the registry ), therefore, the entire directory is copied and verified. You can verify it using the following statement:

NPM list

As a result, the environment has finally been set up. You can start the following work.

Several precautions and summary

1. The module path of nodejs is the node_modules subdirectory under the nodejs installation directory. For example

C: \ Program Files \ nodejs \ node_modules

2. Many commands must be executed by node, such

Node-V

Node-help

Node is a command line execution program, which can be found in the root directory of nodejs

3. Many Commands require NPM execution. You can find the NPM. CMD file in the root directory of nodejs.

4. The help documentation is very important. It is often more accurate than Google in locating problems.

5. There are two concepts: finding answers and making a blind attempt.

6. It is always good to make too many moves, so never be afraid of them.

 

Reference:

Http://twaver.servasoft.com /? P = 3426

Http://cnodejs.org

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.