进程为程序的运行提供必备有环境。 进程就相当于工厂中的车间。
线程。计算机中最小的计算单位,线程负责执行进程中的程序。 线程就相当于工厂中的工人。
Node.js 是一个能够在服务器端运行的 JavaScript 的开放源代码、跨平台 JavaScript 运行环境。
Node 采用 Google 开发的 V8 引擎运行代码,使用事件驱动、非阻塞和异步 I / O 模型等技术来提高性能,可优化应用程序的传输量和规模。
Node 在部分基本模块都用 JavaScript 编写。在 Node 出现之前,js 通常作为客户端设计语言使用,以 js 写出的程序常在用户浏览器上运行。
目前,Node 已被 IBM、Microsoft、Yahoo!、WaImart、Groupon、SAP、LinkedIn、Rakuten、PayPaIs、Voxer 和 GoDaddy 等企业采用。
传统的服务器都是多线程的。每进来一个请求,就创建一个线程去处理请求。
Node 的服务器是单线程的。Node 处理请求时是单线程的。但是在后台有一个 I/O 线程池。
http://nodejs.cn/learn/how-to-install-nodejs
geek@geek-PC:~/Desktop/nodejs$ vim hello.js
console.log('hello, Node'); var a = 123; var b = 456; console.log(a + b); geek@geek-PC:~/Desktop/nodejs$ node hello.js hello, Node 579如果程序设计的规模达到了一定程度,则必须对其进行模块化。
模块化可以有多种形式,但至少应该提供能够将代码分割为多个源文件的机制。
CommonJS 的模块功能可以帮我们解决该问题。
CommonJS 规范的提出主要是为了弥补当前 js 没有标准的缺陷
CommonJS 规范为 js 指定了一个美好的愿景,希望 js 能够在任何地方运行
CommonJS 模块的定义十分简单。
模块引用。 模块定义。 模块标识。
module_01.js。 /* * 模块化。 * 在 node 中,一个 js 文件就是一个模块。 * * 在 node 中,每一个 js 文件中的 js 代码都是独立运行在一个函数中的。 * 而不是全局作用域。 * */ console.log("我是一个模块。"); var x = 10; var y = 20; // 向外部暴露属性或方法。exports。 exports.x = "我是 module_01.js 中的 x。"; exports.y = '我是 y。' module_02.js。 // 引入其他的模块。 /* * 在 node 中,通过 require() 函数来引入外部的模块。 * require() 可以传递一个文件的路径作为参数,node 将会自动根据该路径来引入外部模块。 * 如果使用相对路径,必须以 . 或 .. 开头。 * * 使用 require() 引入模块,该函数会返回一个对象,这个对象代表的是引入的模块。 * */ var require1 = require("./module_01");/* 我是一个模块。*/ // console.log(x)/* ReferenceError: x is not defined*/ // console.log(require1.x)// undefined。 // exports.x = "我是 module_01.js 中的 x。"; // exports.y = '我是 y。' console.log(require1.x);/* 我是 module_01.js 中的 x。*/ console.log(require1.y);/* 我是 y,*/ math.js。 exports.add = function (a, b) { return a + b; } exports.mul = function (a, b) { return a * b; } var require1 = require('./math'); console.log(require1.add(1, 2))// 3 console.log(require1.mul(1, 2))// 2使用 require() 引入外部模块时,使用的就是模块标识。可以通过模块标识来找到指定的模块。
由 node 引擎提供的模块。
核心模块的标识就是模块的名字。(不需要写路径)。
由用户自己创建的模块。
使用相对路径或绝对路径。
(栈内存 v.s. 堆内存)。
exports 是对象。存在堆内存。 只能使用 . 的方式向外暴露内部变量。
module.exports 是对象的属性(变量)。存在堆内存。 既能使用 . 的方式,也可以通过直接赋值。
exports.name = '孙悟空'; exports.age = 18; exports.sayName = function () { console.log('我是孙悟空。。。'); } var hello = require('./helloModule'); console.log(hello.name); // exports.name = '孙悟空'; // module.exports.name = '孙悟空'; // exports.age = 18; // module.exports.age = 18; // exports.sayName = function () { // module.exports.sayName = function () { // console.log('我是孙悟空。。。'); // } module.exports = { name: '猪八戒', age: 28, sayName: function () { console.log('我是猪八戒'); } }CommonJS 包规范允许我们将一组相关的模块组合到一起,形成一组完整的工具。
CommonJS 的包规范由包结构和包描述文件两个部分组成。
包结构。 用于组织包中的各种文件。 包描述文件。 描述包的相关信息,以供外部读取分析。package.json。 描述文件。
bin。 可执行二进制文件。
lib。 js 代码。
doc。 文档。
test。 单元测试。
(软件管家、Yum、apt、maven)。
geek@geek-PC:~$ npm -v 5.8.0 geek@geek-PC:~$ npm npm WARN npm npm does not support Node.js v10.19.0 npm WARN npm You should probably upgrade to a newer version of node as we npm WARN npm can't make any promises that npm will work with this version. npm WARN npm Supported releases of Node.js are the latest release of 4, 6, 7, 8, 9. npm WARN npm You can find the latest version at https://nodejs.org/ Usage: npm <command> where <command> is one of: access, adduser, bin, bugs, c, cache, ci, completion, config, ddp, dedupe, deprecate, dist-tag, docs, doctor, edit, explore, get, help, help-search, i, init, install, install-test, it, link, list, ln, login, logout, ls, outdated, owner, pack, ping, prefix, profile, prune, publish, rb, rebuild, repo, restart, root, run, run-script, s, se, search, set, shrinkwrap, star, stars, start, stop, t, team, test, token, tst, un, uninstall, unpublish, unstar, up, update, v, version, view, whoami npm <command> -h quick help on <command> npm -l display full usage info npm help <term> search for help on <term> npm help npm involved overview Specify configs in the ini-formatted file: /home/geek/.npmrc or on the command line via: npm <command> --key value Config info can be viewed via: npm help config npm@5.8.0 /usr/share/npm geek@geek-PC:~$ geek@geek-PC:~$ sudo n stable installing : node-v12.18.2 mkdir : /usr/local/n/versions/node/12.18.2 fetch : https://nodejs.org/dist/v12.18.2/node-v12.18.2-linux-x64.tar.xz installed : v12.18.2 (with npm 6.14.5) Note: the node command changed location and the old location may be remembered in your current shell. old : /usr/bin/node new : /usr/local/bin/node To reset the command location hash either start a new shell, or execute PATH="$PATH"会自动生成一个 package.json。
此时可以下载其他包了。
geek@geek-PC:~/geek/npm_geek/helloNpm$ npm install math npm notice created a lockfile as package-lock.json. You should commit this file. npm WARN hello_npm@1.0.0 No repository field. + math@0.0.3 added 1 package from 1 contributor and audited 1 package in 2.325s found 0 vulnerabilities geek@geek-PC:~/geek/npm_geek/helloNpm$ tree . ├── node_modules │ └── math │ ├── AUTHORS │ ├── Cakefile │ ├── math.coffee │ ├── math.js │ └── package.json ├── package.json └── package-lock.json 2 directories, 7 files 使用 Math。 geek@geek-PC:~/geek/npm_geek/helloNpm$ vim index.js geek@geek-PC:~/geek/npm_geek/helloNpm$ cat index.js let math = require('math'); console.log(math) geek@geek-PC:~/geek/npm_geek/helloNpm$ node index.js Object [Math] { samesign: [Function], copysign: [Function], add: [Function], sum: [Function], mul: [Function], prod: [Function], factorial: [Function], gcd: [Function], lcm: [Function] } geek@geek-PC:~/geek/npm_geek/helloNpm$ cat index.js let math = require('math'); console.log(math); console.log(math.add(111, 111)); geek@geek-PC:~/geek/npm_geek/helloNpm$ node index.js Object [Math] { samesign: [Function], copysign: [Function], add: [Function], sum: [Function], mul: [Function], prod: [Function], factorial: [Function], gcd: [Function], lcm: [Function] } 222中国人自己的镜像服务器。
淘宝 NPM 镜像。
这是一个完整 npmjs.org 镜像,你可以用此代替官方版本(只读),同步频率目前为 10 分钟 一次以保证尽量与官方服务同步。
https://developer.aliyun.com/mirror/NPM?from=tnpm
使用说明。
你可以使用我们定制的 cnpm (gzip 压缩支持) 命令行工具代替默认的 npm:
$ npm install -g cnpm --registry=https://registry.npm.taobao.org
或者你直接通过添加 npm 参数 alias 一个新命令:
alias cnpm="npm --registry=https://registry.npm.taobao.org \ --cache=$HOME/.npm/.cache/cnpm \ --disturl=https://npm.taobao.org/dist \ --userconfig=$HOME/.cnpmrc" # Or alias it in .bashrc or .zshrc $ echo '\n#alias for cnpm\nalias cnpm="npm --registry=https://registry.npm.taobao.org \ --cache=$HOME/.npm/.cache/cnpm \ --disturl=https://npm.taobao.org/dist \ --userconfig=$HOME/.cnpmrc"' >> ~/.zshrc && source ~/.zshrc通过 npm 下载的包都放到 node_module 文件夹中。
node 在使用模块名来引入模块时,会首先在当前目录的 node_modules 中寻找是否含有该模块。
如果有则直接使用,如果没有则去上一级目录的 node_modules中寻找。
如果有则直接使用,如果没有则再去上一级目录寻找,知道找到为止。
知道找到磁盘的根目录,如果依然没有则报错。
Buffer 缓冲区。
Buffer 的结构和数组很像,操作的方法也和数组类似。
数组中不能存储二进制文件,而 buffer 就是专门用来存储二进制数据的。
从结构上看 Buffer 非常像一个数组,ta 的元素为十六进制的两位数。 实际上一个元素就表示内存中的一个字节。 实际上 Buffer 中的内存不是通过 JavaScript 分配的,而是在底层通过 C++ 申请的。 我们可以直接通过 Buffer 来创建内存中的空间。
/* Buffer 缓冲区。 Buffer 的结构和数组很像,操作的方法也和数组类似。 数组中不能存储二进制文件,而 buffer 就是专门用来存储二进制数据的。 */ let str = '李'; // 将一个字符串保存到 buffer 中。 let buf = Buffer.from(str); console.log(buf); // <Buffer 48 65 6c 6c 6f 20 47 65 65 6b> // let str = 'Hello Geek'; // console.log(str.length);// 10。占用内存的大小。 // console.log(buf.length);// 10。字符串的长度。 // let str = '李'; console.log(str.length);// 1。占用内存的大小。 console.log(buf.length);// 3。字符串的长度。 // buffer 中存储的都是二进制数据。 // 计算机显示十六进制数据。 // buffer 中的每一个元素的范围是 00 - ff(0 ~ 255) 00000000 ~ 11111111。 // 创建指定大小的 buffer。 // let buffer = new Buffer(10); // console.log(buffer.length); // 构造函数不推荐使用。 let buf2 = Buffer.alloc(10); console.log(buf2); // <Buffer 00 00 00 00 00 00 00 00 00 00> let buf3 = Buffer.allocUnsafe(10); console.log(buf3); // <Buffer 01 00 00 00 00 00 00 00 2e e1> let buf4 = Buffer.from('我是一段文本数据'); console.log(buf4); console.log(buf4.toString()); // <Buffer e6 88 91 e6 98 af e4 b8 80 e6 ae b5 e6 96 87 e6 9c ac e6 95 b0 e6 8d ae> // 我是一段文本数据服务器的本质就是将本地的文件发送给远程的客户端。
const fs = require(‘fs’);
fs 中所有的操作都有两种形式可供选择。同步和异步。
同步 fs 会阻塞程序的执行,也就是除非操作完毕,否则不会向下执行代码。
异步文件系统不会阻塞程序的执行,而是在操作完成时,通过回调函数将结果返回。