{"id":22283764,"url":"https://github.com/veaba/learn-node","last_synced_at":"2025-03-25T19:51:35.739Z","repository":{"id":104582192,"uuid":"165166078","full_name":"veaba/learn-node","owner":"veaba","description":"learn node.js","archived":false,"fork":false,"pushed_at":"2019-12-30T08:31:17.000Z","size":28,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-01-30T17:39:39.862Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/veaba.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-01-11T02:34:37.000Z","updated_at":"2019-12-30T08:31:19.000Z","dependencies_parsed_at":null,"dependency_job_id":"79e42920-5711-407f-9397-984b7b55a5d2","html_url":"https://github.com/veaba/learn-node","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/veaba%2Flearn-node","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/veaba%2Flearn-node/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/veaba%2Flearn-node/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/veaba%2Flearn-node/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/veaba","download_url":"https://codeload.github.com/veaba/learn-node/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245535440,"owners_count":20631293,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-03T16:42:05.249Z","updated_at":"2025-03-25T19:51:35.711Z","avatar_url":"https://github.com/veaba.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"## 学习 node.js\n\n## 学习一些api 的方法和使用\n\n\n## node线程\n- node 是单线程进程\n    - 特点：\n        - JS引擎只有一个实例\n        - 事件驱动处理IO等异步\n        - 可能更适合IO密集型操作\n    - 优点：减少线程间切换的开销\n    - 缺点：\n        - 无法让主线程进行CPU密集型操作，会阻塞主线程\n    - cpu 密集型（计算），可以通过child_process 创建独立的子进程，父子进程通过IPC通信（进程间通信），子进程可以是外部程序，结果返回给父进程\n    - IPC通信：进程间通信，科普：https://www.cnblogs.com/CheeseZH/p/5264465.html\n\n    - node 不是单线程：严格说，node不是单线程！\n        - js引擎执行的线程\n        - 定时器线程（setTimeout,setInterval）\n        - 异步http线程（ajax）\n    - node 一般单线程说法：\n        - 只有一个JS引擎在主线程上运行\n        - 其他异步IO和事件驱动相关线程通过libuv实现内部的线程池和线程调度   \n        - libuv 存在 Event Loop，通过它来实现多线程效果\n        - Event Loop：维持一个执行栈和一个事件队列\n    - node 单进程：线程是最小进程，因此node 也是单进程。（node 是单进程、单线程的原因）\n- CPU密集\n    - 计算、逻辑判断\n- IO密集操作\n    - 网络通讯、网络传输、磁盘读写\n- libuv：跨平台高性能、事件驱动的I/O库\n- node 提供 child_process 模块实现多进程\n    - 子进程：也叫工作进程，worker\n        - 可以调用非Node命令，如python、shell、以流或回调形式返回\n    - 主进程：master进程\n\n\n### child_process API\n\n- spawn：非Node应用、流的形式返回、不能直接运行shell命令\n- execFile：非Node应用、流的形式返回\n\n|类型|spawn|execeFile|exec|fork|\n|----|----|----|----|----|\n|是否Node应用|否|否|否|是|\n|返回结果|流|回调|回调|流|\n|是否直接执行shell|否|否|是|否|\n\n#### exec \n- 安全性比较低\n```js\nconst cp= require('child_porcess')\ncp.execFile('echo hello world'],functionc(err,stdout){\n    console.log(setout)\n})\n```\n#### execFile\n- 安全性高\n- 会检查入参安全性\n```js\nconst cp= require('child_porcess')\ncp.execFile('echo',['hello','world'],functionc(err,stdout){\n    console.log(setout)\n})\n```\n\n#### spawn\n\n```js\nconst cp=require('child_process')\nconst cat= cp.spawn('cat',['input.txt'])\nconst sort=cp.spawn('sort')\nconst uniq=cp.spawn('uniq')\n\ncat.stdout.pipe('sort.stdin')\nsort.stdout.pipe('uniq.stdin')\nuniq.stdout.pipe(process.stdout)\n\nconsole.log(porcess.stdout)\n```\n\n#### fork\n\n- 子进程\n    - process.on('message')接收消息、process.send()发送消息\n\n- 父进程\n    - child.on('message')接收消息、child.send() 发送消息","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fveaba%2Flearn-node","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fveaba%2Flearn-node","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fveaba%2Flearn-node/lists"}