This article is the first in lei’s NodeJS series.

Column description: “Lei Xuewei NodeJS hobby series” for interest hobby series and write 15 years to listen to a lot of NodeJS technology to share, for a time on the head, used to rapid development these two years just want more external sharing, also as interest small article solemn statement, lei Xuewei any article banned republication!

About NodeJS

NodeJS is a JavaScript runtime environment based on Chrome V8 engine. It is a development platform that allows JavaScript to run on the server.

It is used to conveniently build network applications with fast response speed and easy to expand.

Other concepts don’t matter, as a novice, just remember that NodeJS runs in an environment based on an engine that the node program below starts.

NodeJS is a code executor. We will use this to run all the code we write.

The code is in a language the NodeJS software can read, just like we speak Chinese and English.

But NodeJS is foreign, it does not understand Chinese by nature, so when writing new code, try not to use Chinese or Chinese input method (this may make your code Chinese, error)!

Front end students often see the command: NPM run start or NPM run dev, etc.

Then a browser window automatically opens and loads the front-end UI code compiled for the current project. This process runs the NodeJS engine.

Let’s install the Node software and use the Node program.

Download and Install

Nodejs.org/zh-cn/downl… or 

Nodejs.org/zh-cn/downl…

Select the latest version. Select Exe for the Window operating system.

You are advised to use zip or tar.gz, and then refer to the following method to set the environment variable PATH.

set PATH=%NODEJS_HOME%:%PATH%
Copy the code

By doing this manually, you’ll be more familiar with node/ NPM configuration. Later playing vue CLI or Angular, react-Script is very helpful.

Run the following command to check whether the installation is successful:

Verify the installation on the Macbook

The version used in this article can be downloaded here: nodejs.org/download/re… , Apple Computer macbook choose PKG bag

Once installed, open CMD or terminal on a MAC and type Node

This leads directly to the interaction terminal that connects to the NodeJS engine.

Note to small white: interactive terminal is online write code to run the code window

Special note: do not use Chinese symbols when entering the program below, because NodeJS is born abroad, and certainly does not understand Chinese, running error!

Start writing NodeJS code

Write the first line of code

Copy the following line directly:

Console. log(" Hello, Lei School Committee! )Copy the code

The effect is as follows, or try another expression: 1+1

 

NodeJS to do a word problem

In mathematics, variables are defined as follows:

Given x=4 and y=20, how many x’s can y be obtained by adding?

This word problem you have seen, let’s use programming to express.

Let’s go ahead and type the following three lines in the above Node window:

x=4
y=20
u=y/x
Copy the code

Isn’t it convenient that the value of the variable u, 5, is the answer to our problem!

These are just simple uses.

NodeJS to do a function

In mathematics, we’ve all learned the idea of a function, that y is equal to 2x plus 5 is a line.

You can figure out what y is if you know any value of x. So how do we do this with the program?

Okay, try it yourself.

Create a new project

What is a project?

A project is simply a folder that contains code files (such as files ending in JS).

We think of the whole directory of files as a whole is the project.

A good project can also be shared with programmers to use! Usually shared to others of the project we called the library (code library), unified after the JS library refers to.

For a brief look at the concept, open the command terminal and type: NPM init

As shown below, the process prompts a number of input options, focusing on package and version.

These two things are like id cards that allow other developers to find your project.

Nodejs sample project

Why does a project need to be given a package and version number?

What if a lot of homework exam questions have a package and version to locate?

If I want to find the answer to a question, I can locate it by pacakge + version.

In the NodeJS development community, there is a website called NPM Registry: www.npmjs.com/.

This site registers all NodeJS open source JS libraries to share with others, so check them out when you have time.

If we want to reuse functionality that someone else’s project has done well, we can locate it through the package and version number.

Through these two locations to find someone else’s code to use, equivalent to copy homework, others write a correct version to you oh

(Always remember to “like” and “repost” support, this will encourage more people to publish and share more JS libraries)

NPM searches for a package

When our project needs to reference other JS libraries, we can specify a “library @version” for positioning.

Here’s an example of package.json dependencies that references koA.

References to other JS library/NPM package examples

 

Develop your first NodeJS project

Write app.js or index.js

Package. json for the sample project

When we created the project, we specified the app.js file as the entry by setting the main property.

So here we write an app.js, as shown in the following figure,

Console. log is a built-in JS function provided by NodeJS that we can use to print content to the terminal.

It can then be executed using node app.js. Remember to execute a JS file using the command: node filenames.js

This is a simple JS library, just used to print content, we do not reference other JS libraries.

Further engineering

Many NodeJS projects can usually use NPM Start or NPM Run start.

Here we can make changes in package.json, as shown in line 8 of the figure below.

 

“NPM run start” or earlier versions of “NPM start”, nodejs parses package.json to find the “start” attribute under the scripts property, equivalent to executing node app.js.

It is common to see that many projects have “build “, “test”, “build-prod” and so on for nodeJS project build tests. You can try adding more scripts word attribute validation yourself.

To start with, above is the first NodeJS project in action. The example is simple: app.js writes the code to call console.log and output “Hello nodejs 001”.

Review the project

We open package.json, which defines the name, version, and description of the project.

This file also defines the program entry (main) and other information such as the author project address.

Package. json can also define some of the js libraries referenced by the project (dependent JS projects).

This will be shown in the next article, along with an example of a nodeJS API that provides the data interface.

The current sample project warehouse: codechina.csdn.net/geeklevin/n… .

Continuous learning and continuous development, I am the Lei School committee! Programming is fun, but the key is to get the technology straight. Creation is not easy, please support a lot, click like collection to support the school committee!

Reference links:

Nodejs.org/dist/latest…

Nodejs.org/en/docs/gui…