Today is the second day of my learning Node.js. The so-called node.js is actually the language of a server written in JavaScript. At the same time, it is a backend framework and an open platform.
1. Related theoretical knowledge:
We can use require to introduce modules and export modules using module.exprorts.
Install nodejs and configure npm
1. Install nodejs, and after installation, type node -v in the cmd command line to view the version.
2. Click npm config list in cmd to check whether npm is installed.
3. Set the mirror address.
Taobao mirror: type npm config set registry=https://registry.npm.taobao.org in the command line
Langwo Server Mirror: Typing npm config set registry=http://192.168.8.10:7001 in the command line
Install 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 project skeleton.
4. Enter npm install in the command to download all plugins that express depends on.
5. Use npm start to start the server. Or node app.
When entering the above command, you must be careful, otherwise it is easy to type incorrectly. At the same time, when generating express, we must first enter a specific folder and then perform corresponding operations.
Here are some module methods:
1. http
2. fs
3. http.createServer is used to create a server
4. listen (Fill in the port number of an application)
5. res.end() This is an ending method we must add when writing node.js
And it can send any data such as a string, except for an array.
2. Related operations of node.js
1. The implementation principle of node.js:
Simply put: node.js is when the number of users is large, the server will put the received user information in the event queue, and then the event queue mechanism will process the user requests every day. For example, using the callback function, you will find one corresponding method after another and execute it. Then after processing, it responds to the browser.
2. Get data from the web page in node.js
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 successfully');});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 successfully');});In the above variable pwd=req.body.pwd, here pwd refers to the value of the name attribute in the form form.
The above article uses nodejs implementation principles and server building (dynamics) is all the content I share with you. I hope you can give you a reference and I hope you can support Wulin.com more.