Node.js 诞生于 2009 年,Node.js 采用 C++ 语言编写而成,是一个基于 Chrome V8 引擎的 JavaScript 运行环境,让 JavaScript 的运行脱离浏览器,可以使用 JavaScript 语言编写服务器端代码
Node.js 官网下载稳定版本,偶数版本为稳定版本
,奇数版本为尝鲜版本
Node.js 中文文档
NPM 官网
NPM 中文文档
Yarn 官网
Yarn 中文文档
Koa 官网
Koa 中文文档
初体验
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| const http = require('http')
const server = http.createServer((req, res) => { res.end('Hello World!') }) server.listen(3000)
const express = require('express')
const app = express() app.get('/', (req, res) => { res.send('Hello World!') }) app.listen(3000)
const Koa = require('koa')
const app = new Koa() app.use(async ctx => { ctx.body = 'Hello World!' }) app.listen(3000)
|
npm 命令
npm init
引导创建一个 package.json 文件
npm help(npm -h) 查看 npm 帮助信息
npm version(npm -v) 查看 npm 版本
npm search 查找 package 包
npm install
(npm i) 安装 package 包
- npm install package_name -S(–save)
- npm install package_name -D(—save-dev)
- npm install package_name -g 全局安装
- npm i package_name@1.2.3
安装指定版本
npm update(npm -up) 更新
npm uninstall(remove) 删除
npm root 查看包安装路径
缓存
- npm config get cache
- yarn cache dir
- pnpm store path
- pnpm store prune
- yarn cache clean –force
- npm cache clean –force
- name 包名
- version 版本
main
入口程序
- scripts 执行脚本
- dependencies 运行依赖
- devDependencies 开发依赖
多进程
child_process.fork
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| const http = require('http') const fork = require('child_process').fork
const server = http.createServer((req, res) => { if (req.url === '/get-sum') { console.log('主进程 id', process.pid)
const computeProcess = fork('./compute.js') computeProcess.send('开始计算')
computeProcess.on('message', data => { res.end(data) })
computeProcess.on('close', () => { computeProcess.kill() res.end('error') }) } }) server.listen(3000)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| function getSum() { let sum = 0 for (let i = 0; i < 10000; i++) { sum += i } return sum }
process.on('message', data => { console.log('子进程 id', process.pid) const sum = getSum() process.send(sum) })
|
cluster.fork
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| const http = require('http') const cluster = require('cluster') const os = require('os') const process = require('process')
const workers = {} if (cluster.isMaster) { cluster.on('death', worker => { worker = cluster.fork() workers[worker.pid] = worker })
for (let i = os.cpus().length; i > 0; i--) { const worker = cluster.fork() workers[worker.pid] = worker }
console.log('主进程') } else { const server = http.createServer((req, res) => { console.log('worker ' + cluster.worker.id + ', PID ' + process.pid) res.end('Hello World!') }) server.listen(3000) }
process.on('SIGTERM', () => { for(let pid in workers) { process.kill(pid) } process.exit(0) })
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| const Koa = require('koa') const app = new Koa()
app.use(async (ctx, next) => { await next() const rt = ctx.response.get('X-Response-Time') console.log(`${ctx.method} ${ctx.url} - ${rt}`) })
app.use(async (ctx, next) => { const start = Date.now() await next() const ms = Date.now() - start ctx.set('X-Response-Time', `${ms}ms`) })
app.use(async ctx => { ctx.body = 'Hello World!' })
app.listen(3000)
|