Using the realization principle of nodejs and building the server of dynamic

  • 2021-07-09 07:14:58
  • OfStack

Today is the second day of my study of Node. js. The so-called node. js is actually the server language written by javascript. At the same time, it belongs to the background framework and is an open platform.

1. Relevant theoretical knowledge:

We can use requre to import modules and module. exprorts to export modules.
Installing nodejs and configuring npm

1. Install nodejs, then type node-v on the cmd command line to see the version.
2. Type npm config list in cmd to see if npm is installed.
3. Set the mirror address.

Taobao Mirror: Type npm config set registry=https://registry.npm.taobao.org on the command line

Longwall server image: Type npm config set registry=http://192.168.8.10: 7001 on the command line

Installing express

1. Initialize the project. Command: npm init

2. Install the global express generation tool. Command: npm install express-generator-g

3. Execute the express command to generate the skeleton of the project.

4. Enter npm install in the command to download all plug-ins that express depends on.

5. Start the server using npm start. Or node app.

When entering the above command, 1 must be careful at 1 point, otherwise it is easy to knock wrong. At the same time, when generating express, we 1 must first enter a specific folder, and then carry out corresponding operations.

Here are some modular methods:

1. http
2. fs
3. http. createServer is used to create a server
4. listen (fill in the port number of 1 application)
5. res. end () This is a closing method that we must add when writing node. js

It can send any data such as a string, except an array.

2. Related operations of node. js

1. The implementation principle of node. js:

Simply put: node. js means that when there is a large number of users, the server will put the received user information in the event queue, and then the event queue mechanism will process the user request every day, for example, using the callback function, find one method after another and execute it. Then, after processing, it responds to the browser.

2. node. js obtains the data in the web page

1 > get method:


router.get('/login',function(req,res){
var username=req.query.username;
var pwd=req.query.pwd;
 console.log(username,pwd);
res.send(' Login Successful ');
});

2 > post method:


router.get('/login',function(req,res){
var username=req.body.username;
var pwd=req.body.pwd;
console.log(username,pwd);
res.send(' Login Successful ');
});

In the above variable pwd = req. body. pwd, where pwd refers to the value of the name attribute in the form form.


Related articles: