node学习:常用终端命令、环境变量、进程与线程和node模板的使用和真相详解

1. cmd(终端)

1.1 常用命令

          dir  =》 列出当前目录下所包含的所有文件

          cd 目录名  =》 进入指定的目录

C:\Users\user>dir 
C:\Users\user>cd Desktop
C:\Users\user\Desktop>

          md 目录名  =》 当前目录下创建一个文件夹, 可以通过dir命令进行确认其存在性。

C:\Users\user\Desktop>md hello
C:\Users\user\Desktop>dir

          rd 目录名  =》 当前目录下删除对应的文件夹, 可以通过cd命令进行确认其存在性。

C:\Users\user\Desktop>rd hello
C:\Users\zhuj02\Desktop>cd hello
系统找不到指定的路径。

          d:   =》 进行D盘根目录,其他根目录亦是。

C:\Users\zhuj02\Desktop>e:
E:\>c:
C:\Users\zhuj02\Desktop>d:
D:\>

1.2 目录

.表示当前目录
..表示上一级目录

 

 

 

C:\Users\user\Desktop>cd .  // 结果:下一行
C:\Users\user\Desktop> 
C:\Users\user\Desktop>cd ..  // 结果:下一行
C:\Users\user>

1.3 环境变量(windows系统变量)

      首先,在桌面上新建hello2文件夹,hello2文件夹中创建一个hello2.txt文件,里面输入内容。进入到hello2目录下,直接在命令行输入hello2.txt就可以直接打开该文件。

      方式二:在不是hello2目录下,想要打开hello2.text文件,由于当前目录下(Desktop)不存在hello2.txt文件,直接在命令行输入hello2.txt文件,结果会打不开,且输出提示文“'hello2.txt' 不是内部或外部命令,也不是可运行的程序
或批处理文件。”。如果想在其他目录下打开hello2.txt文件,就需要用到环境变量,在系统属性-高级-环境变量中为path新增一个hello2.txt目录值,即可(需重新打开cmd执行)。原理:命令行打开某文件或调用某程序时,系统会首先在当前目录下寻找该文件,如果找到即直接打开,如果没有找到,会依次在环境变量中的path中寻找,直到找到为止。(类似于作用域链的查询)

C:\Users\user\Desktop>cd hello2

C:\Users\user\Desktop\hello2>dir
 驱动器 C 中的卷是 Windows
 卷的序列号是 B48A-46D7

 C:\Users\user\Desktop\hello2 的目录

2021-05-11  14:35    <DIR>          .
2021-05-11  14:35    <DIR>          ..
2021-05-11  14:35                43 hello2.txt
               1 个文件             43 字节
               2 个目录 44,273,590,272 可用字节
C:\Users\user\Desktop\hello2>hello2.txt
C:\Users\zhuj02\Desktop\hello2>cd ..
C:\Users\zhuj02\Desktop>hello2.txt
'hello2.txt' 不是内部或外部命令,也不是可运行的程序
或批处理文件。

2. 进程和线程

      进程:负责为程序的运行提供必备的环境;相当于工厂的车间。当一个程序开始运行时,它就是一个进程,进程包括运行中的程序和程序所使用到的内存和系统资源。而一个进程又是由多个线程所组成的。

      线程:程序中的一个执行流,计算机最小的计算单位,负责执行进程中程序;相当于工厂中 的工人。不同的线程可以执行同样的函数。

      单线程:一个进程中只有一个线程在执行程序。js是单线程

      多线程:程序中包含多个执行流,即在一个程序中可以同时运行多个不同的线程来执行不同的任务,也就是说允许单个程序创建多个并行执行的线程来完成各自的任务。

3. node

3.1 简介

      Node.js 是一个能够在服务器端运行JavaScript的开放源代码、跨平台JavaScript运行环境。采用Google开发的V8引擎运行js代码,使用事件驱动非阻塞异步I/O模型等技术提高性能,可优化应用程序的传输量和规模。在node.js中js可以直接和系统进行交互,作用与c\c++\java等一样。

      Node.js 大部分基本模型都用JavaScript编写。在Node出现之前,JS通常作为客户端程序涉及语言使用,以JS写出的程序常用在用户的浏览器上运行。

      Node单线程,所有请求使用一个线程处理。

3.2 在node环境中执行js代码块

      首先需要打开cmd命令行窗口,输入node进入node环境中,可以输入js代码执行了。

C:\Users\user>node
> console.log(123)
123
undefined
> for (var i = 0; i < 10; i++) {
... console.log(i);
... }
0
1
2
3
4
5
6
7
8
9
undefined
>

3.3 node执行js文件

      在cmd窗口中,进入js文件所在的目录中,通过  node js文件名  命令进行执行js文件。

C:\Users\user\Desktop\hello2>node hello2.js
123
0
1
2
3
4
5
6
7
8
9

3.4 模块化-基本使用

      在node中一个js文件就是一个模块。当进行项目制作时,如果实现的功能较为复杂,可以将部分功能整体抽出来当作一个模块进行开发,有利于后期维护,降低项目中代码的耦合。如果在本项目的其他板块或后期开发其他项目时,遇到相同或类似的功能时可以直接使用该模块,提高了代码的复用率。

      在node中,A模块中通过 require( url ) 引入B模块。url如果使用相对路径,必须以.或..开始。

// - A_module.js
// - B_module.js
// A_module.js引入B_module.js模块
require("./B_module.js")

      使用 require( url ) 引入模块后,该函数会返回一个对象,这个对象代表的是引入的模块。在node中,每一个js文件中的js代码都是独立运行在一个函数中,而不是全局作用域,一个模块中 的变量和函数在其他模块中无法被访问,对外不可见。如果直接在A模板中直接使用B模板的数据,会输出打印undefined,读取失败。

// B_module.js
console.log('B-模块')
var x = 10
var y = 20
// A_module.js
var mb = require("./B_module.js")
console.log(mb.x)
// 终端执行
PS F:\node\js> node A_module.js
B-模块
undefined

      如果需要向外暴露属性或方法,可以通过 exports 或module.exports来实现。

// B_module.js
var z = 'z-test-exports'
exports.z = z
// module.exports.z = z

// A_module.js
var mb = require("./B_module.js")
console.log(mb.z)
// 终端执行
PS F:\node\js> node A_module.js
z-test-exports

3.5 模块化-exports

      自己在测试了module.exports与exports,发现了他们之间的关系是module.exports === exports ,且exports是module.exports的引用,所以当module.exports与exports进行比较时,输出的结果时true。从终端输出的module中的组成就可以看出module是对象,并且exports只是module中所包含的一个对象。

// B_module.js
var z = 'z-test-exports'
exports.z = z
console.log(module)
console.log(module.exports === exports) // true
// A_module.js
var mb = require("./B_module.js")
PS F:\workspace\yanj\RD4006\cssTest\node\js> node A_module.js
Module {
  id:
   'F:\\workspace\\yanj\\RD4006\\cssTest\\node\\js\\B_module.js',
  exports: { z: 'z-test-exports' },
  parent:
   Module {
     id: '.',
     exports: {},
     parent: null,
     filename:
      'F:\\workspace\\yanj\\RD4006\\cssTest\\node\\js\\A_module.js',
     loaded: false,
     children: [ [Circular] ],
     paths:
      [ 'F:\\workspace\\yanj\\RD4006\\cssTest\\node\\js\\node_modules',
        'F:\\workspace\\yanj\\RD4006\\cssTest\\node\\node_modules',
        'F:\\workspace\\yanj\\RD4006\\cssTest\\node_modules',
        'F:\\workspace\\yanj\\RD4006\\node_modules',
        'F:\\workspace\\yanj\\node_modules',
        'F:\\workspace\\node_modules',
        'F:\\node_modules' ] },
  filename:
   'F:\\workspace\\yanj\\RD4006\\cssTest\\node\\js\\B_module.js',
  loaded: false,
  children: [],
  paths:
   [ 'F:\\workspace\\yanj\\RD4006\\cssTest\\node\\js\\node_modules',
     'F:\\workspace\\yanj\\RD4006\\cssTest\\node\\node_modules',
     'F:\\workspace\\yanj\\RD4006\\cssTest\\node_modules',
     'F:\\workspace\\yanj\\RD4006\\node_modules',
     'F:\\workspace\\yanj\\node_modules',
     'F:\\workspace\\node_modules',
     'F:\\node_modules' ] }
true

      但module.exports与exports还是有区别的。看下段代码:

      首先是module.exports的暴露,把暴露的值正确打印出来了。

// hello.js
module.exports = {
  name: '孙悟空',
  age: '未知',
  sayName: function () {
    console.log("我乃齐天大圣是也!")
  }
}
// A_module.js
var hello = require('./hello')
console.log(hello.name)
console.log(hello.age)
hello.sayName()
PS F:\workspace\yanj\RD4006\cssTest\node\js> node A_module.js
孙悟空
未知
我乃齐天大圣是也!

      接着是exports的暴露,输出打印是undefined。

// hello.js
exports = {
  name: '猪八戒',
  age: '未知',
  sayName: function () {
    console.log("我乃天蓬元帅!")
  }
}
// A_module.js
var hello = require('./hello')
console.log(hello.name)
console.log(hello.age)
hello.sayName()
PS F:\workspace\yanj\RD4006\cssTest\node\js> node A_module.js
undefined
undefined
F:\workspace\yanj\RD4006\cssTest\node\js\A_module.js:29
hello.sayName()
      ^

TypeError: hello.sayName is not a function
    at Object.<anonymous> (F:\workspace\yanj\RD4006\cssTest\node\js\A_module.js:29:7)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

      为什么?

      这种情况,要明白改值,改的是变量还是改对象。此处改的是对象,原先exports指向的是module.exports,但当exports对象修改了之后,exports就不再指向module.exports了,当输出打印暴露的值是,实际输出的是module.exports里的值,当exports对象被修改之后,不再指向module.exports了,exports里的值无法暴露出来。当在A模板中输出打印module.exports和exports时,里面的是空值。

      

3.6 模块化-内置模块path、fs

       对于module中path模板的使用。其中dirname(url)返回的是文件所在文件夹的路径,basename(url)返回的是文件名(如果带有文件格式后缀的)或有'/'分割后的最后一位,extname(url) 返回的是文件后缀。

// A_module.js
var path = require('path')
var dirname = path.dirname('../../es6/es7-11/es8.js')
var dirname_1 = path.dirname('../../es6/es7-11/')
var dirname2 = path.basename('../../es6/es7-11/es8.js')
var dirname2_1 = path.basename('../../es6/es7-11/')
var dirname3 = path.extname('../../es6/es7-11/es8.js')
var dirname3_1 = path.extname('../../es6/es7-11/')
console.log(dirname)
console.log(dirname_1) // ../../es6
console.log(dirname2)
console.log(dirname2_1) // es7-11
console.log(dirname3)
console.log(dirname3_1) // ''
PS F:\workspace\yanj\RD4006\cssTest\node\js> node A_module.js
../../es6/es7-11
../../es6
es8.js
es7-11
.js

      node中内置的fs模块就是文件系统模块,负责读写文件。

      读取文件fs.readFile():

// A_module.js
fs.readFile('./B_module.js', function (err, data) {
  if (err) {
    console.log(err)
  } else {
    console.log(data) // 输出的是Buffer 需要转字符串输出
    console.log(data.toString())
  }
})
PS F:\workspace\yanj\RD4006\cssTest\node\js> node A_module.js
<Buffer 63 6f 6e 73 6f 6c 65 2e 6c 6f 67 28 27 42 2d e6 a8 a1 e5 9d 97 27 29 0d 0a 76 61 72 20 78 20 3d 20 31 30 0d 0a 7
6 61 72 20 79 20 3d 20 32 30 0d 0a 76 ... >
console.log('B-模块')
var x = 10
var y = 20
var z = 'z-test-exports'
exports.z = z

      写入文件fs.writeFile():

//A_module.js
var data = `
    var i = 10;
    exports.test = i;
    exports.add = function (a, b = i) {
      console.log(a + b)
    }
`;
fs.writeFile('./write.js', data , function (err) {
  if (err) {
    console.log(err)
  } else {
    console.log('write OK !', err)
    var write = require('./write')
    write.add(20, 45)
  }
})
PS F:\workspace\yanj\RD4006\cssTest\node\js> node A_module.js
write OK ! null
65
// 通过writeFile()写入后的write.js
// write.js
    var i = 10;
    exports.test = i;
    exports.add = function (a, b = i) {
      console.log(a + b)
    }

      获取文件信息fs.stat():

// A_module.js
fs.stat('./write.js', function (err, stat) {
  if (err) {
    console.log(err)
  } else {
    console.log('isFile: ', stat.isFile())
    console.log('isDirectory: ', stat.isDirectory())
    if (stat.isFile()) {
      console.log('size: ', stat.size)
      console.log('birth time: ', stat.birthtime)
      console.log('modified time: ', stat.mtime)
    }
  }
})
PS F:\workspace\yanj\RD4006\cssTest\node\js> node A_module.js
isFile:  true
isDirectory:  false
size:  110
birth time:  2021-05-13T03:56:40.029Z
modified time:  2021-05-13T05:39:38.562Z

3.7 模块化-模板的真相

      模块标识:在使用require(url)引入外部模块时,url就是模块标识。

      模块的两大类:核心模块(由node引擎提供的模块,fs、path等)和文件模块(由用户自己创建的模块)。核心模块的引入,直接使用require('fs')或require('path')标识模块名即可。像上面的require(url)的引入是文件模块。

      node中有一个全局对象  global,它的作用与网页中的window类似。其中js文件中的全局创建的变量或方法是保存在global或window中的。

// A_module.js
var a = 10
console.log(global.a) // undefined
b = 20
console.log(global.b) // 20
PS F:\workspace\yanj\RD4006\cssTest\node\js> node A_module.js
undefined
20

      var a 为什么不是全局变量?

      因为 a 的定义是局部变量,定义在函数里。首先在该js文件中输出打印arguments,如果arguments存在,这说明a是运行在函数里,因为全局中没有arguments。如下运行结果,表明存在arguments,而且该arguments中存在5个值。

// A_module.js
console.log(arguments.length) // 5
PS F:\workspace\yanj\RD4006\cssTest\node\js> node A_module.js
5

      但是,该函数是什么函数呢,存在哪里,什么形式?其实,可以通过arguments的一个callee可以打印该函数,操作和结果如下:

// A_module.js
console.log(arguments.callee + '')
PS F:\workspace\yanj\RD4006\cssTest\node\js> node A_module.js
undefined
20
function (exports, require, module, __filename, __dirname) {
var a = 10
console.log(global.a)
b = 20
console.log(global.b)
console.log(arguments.callee + '')
}

      其实到这里,我们就可以看出,当node在执行模块中的代码时,会首先在代码中的最顶部,添加  function (exports, require, module, __filename, __dirname) {  ,在代码的最底部,添加   }    。将js代码块运行在函数中执行的,当函数执行时,同时传递了5个实参。其实上面说exports是module.exports的引用,从这里也是能看出来的,而在require(url)引入模块时,require也不是全局函数,使用的是函数中的形参require。

      所以,在模块中的var a = 10;并不是全局变量,而是存在函数中的局部变量,当global调用a时,输入的时undefined。其他亦是。

      上面代码中打印arguments.length为5,那arguments中保存的值是什么?先输出打印arguments,看一下里面的内容。

// A_module.js
console.log(arguments)
PS F:\workspace\yanj\RD4006\cssTest\node\js> node A_module.js
[Arguments] {
  '0': {},
  '1':
   { [Function: require]
     resolve: { [Function: resolve] paths: [Function: paths] },
     main:
      Module {
        id: '.',
        exports: {},
        parent: null,
        filename:
         'F:\\workspace\\yanj\\RD4006\\cssTest\\node\\js\\A_module.js',
        loaded: false,
        children: [Array],
        paths: [Array] },
     extensions:
      [Object: null prototype] { '.js': [Function], '.json': [Function], '.node': [Function] },
     cache:
      [Object: null prototype] {
        'F:\\workspace\\yanj\\RD4006\\cssTest\\node\\js\\A_module.js': [Module],
        'F:\\workspace\\yanj\\RD4006\\cssTest\\node\\js\\B_module.js': [Module] } },
  '2':
   Module {
     id: '.',
     exports: {},
     parent: null,
     filename:
      'F:\\workspace\\yanj\\RD4006\\cssTest\\node\\js\\A_module.js',
     loaded: false,
     children: [ [Module] ],
     paths:
      [ 'F:\\workspace\\yanj\\RD4006\\cssTest\\node\\js\\node_modules',
        'F:\\workspace\\yanj\\RD4006\\cssTest\\node\\node_modules',
        'F:\\workspace\\yanj\\RD4006\\cssTest\\node_modules',
        'F:\\workspace\\yanj\\RD4006\\node_modules',
        'F:\\workspace\\yanj\\node_modules',
        'F:\\workspace\\node_modules',
        'F:\\node_modules' ] },
  '3':
   'F:\\workspace\\yanj\\RD4006\\cssTest\\node\\js\\A_module.js',
  '4': 'F:\\workspace\\yanj\\RD4006\\cssTest\\node\\js' }

      从里面的内容可以看出,第二个值是require函数,第三个值是module,第四第五个值明显是路径,那么第一个值就是exports。其实从arguments本身代表的含义中也能知道其里的值是(exports, require, module, __filename, __dirname)。含义:arguments是一个对应于传递给函数的参数的类数组对象。arguments里的值是函数传递的实参列表。

      arguments里值得含义:

  • exports:该对象用来将变量或函数暴露到外部
  • require:函数,用来引入外部得模块
  • module:代表的是当前模块本身
  • __filename:当前模块所在的完整路径
  • __dirname:当前模块所在文件夹的完整路径

 


点击进入下一篇。。。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值