Get started with nodejs in one article (release npm package)

Get started with nodejs in one article

The prerequisite for reading this article is that you have understood the basic grammar of js.
Nodejs is born for the backend.
Simply put, Node.js is JavaScript running on the server.
Node.js is a platform based on the Chrome JavaScript runtime.
Node.js is an event-driven I/O server-side JavaScript environment. Based on Google's V8 engine, the V8 engine executes Javascript very fast and has very good performance.

Advantage

Good performance For the front-end, easy to use
, download and install from the official website
https://nodejs.org/en/

Start from hello world

console.log("hello world");//保存为hello.js

Run: node hello.js

Simplified http server

const http = require("http");
http.createServer(function(req,res){
	//console.log(req.url); 
	// 根据请求的路径自己写路由分发请求,返回对应的内容
	// 这要等后面学习了文件操作以后才可以实现
	res.write('server response');
	res.end();
}).listen(8080);

File operations (read and write)

const fs = require("fs");
var path = __dirname +"/test.txt";
fs.readFile(path,function(err,data){
	if(err){
		console.log('读取失败');
	}else{
		console.log(data.toString());
	}
});
fs.writeFile(path,"The words are writen by nodejs",function(err){
	console.log(err);
});

Use file reading and writing to achieve access to the specified page of the server

const http = require("http");
const fs = require("fs");
http.createServer(function(req,res){
	var baseDir = __dirname+'/www';//网站根目录
	var file = baseDir+req.url;// 指定文件
	//console.log(file);
	fs.readFile(file,function(err,data){
	if(err){
		res.write('404');
	}else{
		res.write(data);
	}
	res.end();
});
}).listen(8080);

The server receives the data submitted by post get (querystring library url library)

Receive get data:

const http = require("http");
const queryString = require("querystring");
const urlLib = require('url');
http.createServer(function(req,res){
	var url = req.url;
	// querystring解析get请求参数
	/*
	var GET = {};
	if(url.indexOf('?')!=-1){
		var qs = url.split('?');
		GET = queryString.parse(qs[1]);
		console.log(GET);
	}
	*/

	// url库解析get请求参数
	var obj = urlLib.parse(url,true);
	console.log(obj);

	res.write('aaa');
	res.end();
}).listen(8080);

Receive post data:

const http = require("http");
const queryString = require("querystring");
http.createServer(function(req,res){
	// 接收解析post数据
	// 当有一段数据到达时触发,数据量大时会分多段传输,这里会被触发多次
	var str = "";// 注意,字符串不能接收所有类型的post数据,比如当用户上传了一个文件时,就不能用字符串接收  
	var i=1;
	req.on('data',function(data){
		console.log(`第${i++}次接收数据`);
		str += data;
	});
	// 当传输结束时触发,这里只会触发一次
	req.on('end',function(){
		var POST = queryString.parse(str);
		console.log(POST);
	});
	res.write('aaa');
	res.end();
}).listen(8080);

Modular

System module

https://nodejs.org/dist/latest-v14.x/docs/api/
There are many system modules that can be used and there are detailed documents available.
Custom modules
To customize a module, we first introduce three knowledge points
require: Contains other modules, for example: const http = require("http");
Load order: system modules> node_modules> custom modules in other paths
exports: external single output, for example: exports.a=1;
module: external batch Output, for example: module.exports={a:1,b:2}

npm package manager

Function:
unified download path,
automatically download dependencies.
Install npm itself. The
nodejs installer will bring its own npm tool.
Use npm to install the package: npm install Package name for example: npm install mysql
Use npm to uninstall the package: npm uninstall Package name for example: npm uninstall mysql
How to publish yourself Package to the npm official website.
First, get familiar with the npm command, here is the document: https://docs.npmjs.com/cli/v6/commands/

Publishing a package requires the following four steps:

  1. Register an account (if you don't have one, you can use npm adduser to register on the command line, or you can register on the official website)
  2. Use npm login to log in at the command line
  3. Initialize npm init in the directory that needs to be packaged and released (here you will be prompted to write some information, one of which is the package name, here is to generate packag.json, after you are familiar with the content in package.json, you can also create it manually One such file replaces the operation of this step)
  4. Publish, npm publish
    after the above four steps, you can search the package just published on the npm official website (https://www.npmjs.com/).
    Publish a new version.
    Modify the version number in packag.json and re-execute npm publish.
    Users can update the version of a package.
    npm update package name

Guess you like

Origin blog.csdn.net/wang740209668/article/details/112344877