likes
comments
collection
share

前端需要去了解的nodejs知识(全局变量)

作者站长头像
站长
· 阅读数 33

千里之行始于足下,九层之台起于累土。一门技术语言的学习过程也是如此,从基础开始一步步深入,每门语言的全局API都是我们入门的开始,前面几篇文章已经对核心的一些API进行了了解,本文主要了解下nodejs的工具模块,以及一些全局API。

全局API

基础变量

  • __dirname,__filename:获取当前目录绝对路径和当前文件绝对路径

全局对象global

全局对象global相当于JavaScript中的window,其上挂载了许多的属性,我们使用Object.getOwnPropertyNames(global)可以查看所有的属性名称,其中我们常用的有很多下面介绍下几个常用并且重要的属性。

console.log(Object.getOwnPropertyNames(global))
[
 *****
  'BigInt64Array',        'DataView',          'Map',
  'BigInt',               'Set',               'WeakMap',
  'WeakSet',              'Proxy',             'Reflect',
  'FinalizationRegistry', 'WeakRef',           'decodeURI',
  'decodeURIComponent',   'encodeURI',         'encodeURIComponent',
  'escape',               'unescape',          'eval',
  'isFinite',             'isNaN',             'global',
  'process',              'Buffer',            'atob',
  'btoa',                 'URL',               'URLSearchParams',
  ******
]

process进程对象

  • 作用:象是一个全局变量,它提供当前 Node.js 进程的有关信息,以及控制当前 Node.js 进程
  • process中有许多进程相关的属性,下面介绍几个常用的属性和方法
    • argv:以数组形式返回nodejs的执行脚本参数
    • platform:运行的操作系统平台
    • version/versions:当前node的版本号/node及相关宝的版本号
    • cwd():当前工作目录
    • uptime():
    // __dirname
    const dirPath = __dirname;
    console.log("dirPath: ", dirPath);
    // __filename
    const filePath = __filename;
    console.log("filePath: ", filePath);
    console.log(process.argv);
    console.log(process.version);
    console.log(process.versions);
    
    setInterval(()=>{
        console.log('the node uptime is: ',process.uptime());
    },2000)
    console.log(process.cwd());
    console.log(process.platform);
    ---------execute-----------
    node demo.js ww
    ---------output-------------
    dirPath:  D:\demo\demo\nodejs\util
    filePath:  D:\demo\demo\nodejs\util\global.js
    [
      'C:\\Program Files\\nodejs\\node.exe',
      'D:\\demo\\demo\\nodejs\\util\\global.js',
      'ww'
    ]
    v16.15.1
    {
      node: '16.15.1',
      v8: '9.4.146.24-node.21',
      uv: '1.43.0',
      zlib: '1.2.11',
      brotli: '1.0.9',
      ***
    }
    D:\demo\demo\nodejs\util
    win32
    this is nextTick
    the node uptime is:  2.0399197
    

注:process许多关于进程的知识,本文不做过多说明。

URL地址对象

在nodejs中URL已逐步取代之前的url.parse,使用方法如下

const {URL} = require('url')

url上的主要信息分为:协议 域名 端口 路径 参数 哈希值 完整的url

const url = require('url');
const {URL} = require('url');
const args = process.argv;
// URL对象和浏览器中的对象相似,安全性更高,弥补了url.parse的缺点
const myUrl = new URL(args[2])
// url.parse已逐步废弃
console.log('url: ', myUrl);
--------------- output  --------------
url:  URL {
  // 完整地址
  href: 'https://blog.csdn.net/fredricen/article/details/106994910',
  // 协议+域名
  origin: 'https://blog.csdn.net',
  // 协议
  protocol: 'https:',
  // 用户名
  username: '',
  // 密码
  password: '',
  // 主机+端口
  host: 'blog.csdn.net',
  // 主机
  hostname: 'blog.csdn.net',
  // 端口
  port: '',
  // 路径
  pathname: '/fredricen/article/details/106994910',
  // 查询参数
  search: '',
  searchParams: URLSearchParams {},
  // 哈希值
  hash: ''
}

setTimeout/clearTimeout:等待执行,多少秒后执行

const timer = setTimeout(() => {
  console.log("this is setTimeout");
}, 1000);
//清楚定时器
clearTimeout(timer)

setImmediate/:约等于setTimeout(fn,0),都是宏任务

setImmediate(() => {
  console.log("this is setImmediate");
});

setInterval/clearInterval:定时执行,每隔多少秒执行一次

const interval = setInterval(()=>{
    console.log('this is interval')
},2000)
//清楚定时器
clearInterval(interval)

除此以外nodejs另外还提供了 Buffer,Math、Event等模块相关变量,此处不再介绍使用时可以查阅API文档。

转载自:https://juejin.cn/post/7138038454390587400
评论
请登录