One of the most detailed notes for nodejs essential basic skills

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


foreword

Today's applications must be accurate and fast to survive the brutal competition. Also, real-time systems need to do things within a certain time window.

Real-time systems using Node.js scale well and can provide longer-lasting results. We will look at the importance of Node.js in real-time systems in detail.

As we know, Node.js can provide continuous two-way communication in various fields. Whether it's a stock trading platform, a social network, or a forum, Node.js can provide an uninterrupted connection for better communication.


提示:以下是本篇文章正文内容,下面案例可供参考

1. Install node

1. Check the node version: node -v .

2. Create a node project

1. Create folder app

2.cmd /app: npm init -y to initialize the node project

3. Run the node file

1、 cmd /app: node index.js

2、 packge.json:

configuration: "scripts": {

"serve":"node index.js", //---------------------

"test": "echo \"Error: no test specified\" && exit 1"

},

3.npm run serve run

4. Set Taobao Mirror

1. cmd administrator:

2.npm config set registry http://registry.npm.taobao.org/

3.npm config get registry View download source

Five, npm package manager, used to install third-party libraries

1.cmd /app:

1. Installation:

npm i pkg1 pkg2 pkg3 install locally

npm i pkg -g global installation

npm root view -g to view the global installation directory

Use npm i pkg -D for development

npm i pkg -S is used after development and launch

npm i pkg@version specified version installation

npm list View plugins

2. Uninstall:

npm uninstall pkg

npm remove pkg

npm r jquery

3. Update:

npm view jquery versions View historical versions

npm update jquery

4. Install and uninstall help commands: 

npm install -h

npm uninstall -h

6. Version introduction:

^3.1.0, the maximum version update is no more than 4.0.0  ^: Indicates that it can be updated

3.1.0: Unable to update

The meaning of the three digits of the XYZ version

X: major version, incompatible update

Y: medium version, add some new content

Z: Minor version, fixed some minor bugs

7. Catalog introduction:

node_modules: addresses of all dependent packages

package.json: project related configuration

scripts: what commands are available in the current project

dependencies: What are the dependencies of the current project

package-lock.json: The cache address of all dependent packages, the next download will be accelerated

npm i : will download all previous cache dependencies

8. Use of templates

1. Third-party modules

npm i pkg

var pkg=require('pkg')

2. Built-in modules

var fs=require('fs')

var http=require('http')

3. Custom modules

Export:

module.exports={'property name': 'property value', 'property name': 'property value'}

import: var utils=require('./utils')

Nine, file reading and writing

1. Synchronous and asynchronous reading

//1、同步读
	var fs=require('fs');
	console.log(1);
	var res=fs.readFileSync("./qiku.txt",'utf-8');
	console.log(res);
	console.log(2);
	//2、异步读取
	console.log(6);
	fs.readFile('./qiku.txt','utf8',function(err,res){
		//如果没有错误结果
		if(!err){
			console.log(res);
		}
	})
	console.log(8);

2. Write synchronously and asynchronously

//同步写
	console.log(11);
	fs.writeFileSync('./html.txt','web大前端')
	 console.log(22);
	//异步写
	console.log(66);
	fs.writeFile('./html.txt','nodejs',function(err){
		  if(!err){
			  console.log("写入成功");
		  }
	})
	console.log(88);

10. Server Construction

//引入http模块
let http=require('http');
//引入url:获取路由参数   npm i url
let url=require('url');

//创建服务
// req:request请求; res:response响应
let server=http.createServer(function(req,res){
	//从请求中拿到路由参数
	let params=url.parse(req.url);
	res.statusCode=200;  //正确的状态吗
	//设置请求头
	res.setHeader('Content-Type','application/json;charset=utf-8')
	//给前端的响应结果
	res.end(`{"errCode":0,"msg":"${params.search}"}`);
})

//开启服务
server.listen(8888,function(){
	console.log("服务启动成功");
})

/* 访问:
     localhost:8888
	 127.0.0.1:8888
	 192.168.13.8:8888
	 查看本机ip:ipconfig 
停止服务:ctrl+c
*/


Summarize


The above is what I want to talk about today. This article only briefly introduces the basic use of nodejs, and nodejs provides a large number of functions and methods that allow us to process data quickly and conveniently.

Guess you like

Origin blog.csdn.net/why0925123/article/details/126840681