Node.js Basics

I. Overview

What can NodeJs do?

  • Lightweight, high-performance web server
  • Front-end and back-end javsscript isomorphic development
  • Convenient and efficient front-end engineering

Two, nodejs architecture

insert image description here

  • The content of the current layer is implemented by js
  • Provide applications that can directly call libraries, such as fs, path, http, etc.
  • The JS language cannot directly operate the underlying hardware settings

insert image description here
bottom layer

  • V8: Execute JS code and provide bridge interface
  • Libuv: event loop, event queue, asynchronous IO
  • Third-party modules: zlib, http, c-area, etc.

insert image description here

3. Why nodejs

Nodejs slowly evolved into a server-side "language"

insert image description here
IO is the slowest link in the computer operation process

insert image description here
Reactor mode, single-threaded to complete multi-threaded work
Reactor mode to achieve asynchronous IO, event-driven
Nodejs is more suitable for IO-intensive high concurrent requests

4. Nodejs asynchronous IO

insert image description here

5. Event-driven

Event-driven, publish-subscribe, observer

publish subscribe

const EventEmitter = require('events')

const myEvent = new EventEmitter()

myEvent.on('事件1', () => {
    
    
  console.log('事件1执行了')
})

myEvent.on('事件1', () => {
    
    
  console.log('事件1-2执行了')
})

myEvent.emit('事件1')

insert image description here

Six, nodejs implements api service

ts-node is a third-party package that runs ts directly

server.ts

// 需求:希望有一个服务,可以依据请求的接口内容返回相应的数据
import express from 'express'
import {
    
     DataStore } from './data'

// console.log(DataStore.list)

const app = express()

app.get('/', (req, res) => {
    
    
  // res.end('1122')
  res.json(DataStore.list)
})

app.listen(8080, () => {
    
    
  console.log('服务已经开启了')
})

ts-node .\server.ts

Seven, nodejs global object

  • __filename: Returns the absolute path of the script file being executed
  • __dirname: returns the directory where the script is being executed
  • Timer class function: the relationship between execution order and event loop
  • process: Provides an interface to interact with the current process
  • require: realize the loading of modules
  • module, exports: processing module export

Eight, the global object process - 1

// 1 资源: cpu 内存
// console.log(process.memoryUsage())
// console.log(process.cpuUsage())

// 2 运行环境:运行目录、node环境、cpu架构、用户环境、系统平台
/* console.log(process.cwd())
console.log(process.version)
// console.log(process.versions)
console.log(process.arch)
console.log(process.env.NODE_ENV)
// console.log(process.env.PATH)
console.log(process.env.USERPROFILE)  // HOME
console.log(process.platform) */

// 3 运行状态: 启动参数、PID、运行时间
console.log(process.argv) // 可以获取执行脚本命令传入的参数
console.log(process.argv0)  // execArgv
console.log(process.pid)   // ppid 


console.log(process.uptime()) //  脚本运行时间

Case md5 encryption of a file

md5.js

const crypto = require('crypto');
const fs = require('fs');

function md51(path) {
    
    
	let buffer = fs.readFileSync(path);
	let fsHash = crypto.createHash('sha1');

	fsHash.update(buffer);
	return fsHash.digest('hex');
}

function md52(path) {
    
    
	let buffer = fs.readFileSync(path);
	let fsHash = crypto.createHash('sha256');

	fsHash.update(buffer);
	return fsHash.digest('hex');
}
console.log(md51(process.argv[2]));
console.log(md52(process.argv[2]));

implementnode md5.js 律动v0_4_3_20220808_mac_M1.zip

Nine, the global object process - 2

script execution event

process.on('exit', (code) => {
    
    
  console.log('exit' + code)
})

process.on('beforeExit', (code) => {
    
    
  console.log('exit' + code)
})

console.log('代码执行完了')

insert image description here

process.exit() //主动退出
console.log(1111) //不会执行

Guess you like

Origin blog.csdn.net/woyebuzhidao321/article/details/124638750