Node.js 是什么
Node.js是一个JS的运行环境,可以让JS运行在服务端的开发平台。可以调用系统接口,开发后端应用。
Node.js并不是后端框架,也就不是像Python那样得编程语言,它只是一个技术集的平台。
应用层 API
前端重点关注的是用Node.js标准库简化 JS 代码的 API ,主要由三部分组成:HTTP模块、fs文件模块、stream模块。
Node.js做了什么
- 用
libuv进行异步I/O操作 - 用
Event Loop管理事件处理顺序(Node.js用于处理:几种不同的事件,同时触发,) - 用
C++库处理DNS/HTTP - 用
bindings让JS能和C++交互通信 - 用
V8运行JS(V8不提供DOM API,交给浏览器操作) - 用Node.js标准库简化JS代码的API
Node.js 文件模块Fs
初始化项目:
yarn init -y
// package.json
{
"name": "demo-1",
"version": "0.1.1",
"main": "index.js",
"license": "MIT"
}
// index.js
console.log('hi')
node index // 执行
yarn add commander@3.0.2
const program = require('commander');
program
.option('-x, --xxx', 'what the x')
program.parse(process.argv);
console.log('here')
node index -h // 打出所有options
const program = require('commander');
program
.option('-x, --xxx', 'what the x')
program
.command('add')
.description('add a task')
.action((...args) => {
const words = args.slice(0,-1).join(' ')
console.log(words);
});
program
.command('clear')
.description('clear all task')
.action((...args) => {
console.log('this is clear');
});
program.parse(process.argv)
console.log(program.xxx)