Node.js 的核心模块

Node.js 的的核心模块

  • Node为JavaScript提供了很多服务器级别的API,这些API绝大多数都被包装到了一个具名的核心模块中了。例如文件操作的fs核心模块,http服务构建的http模块path 路径操作模块、os 操作系统信息模块等
  • 以后只要说这个模块是一个核心模块,就要马上想到如果想要使用它,就必须:
var fs = require(‘fs’)
var http = require('http')

案例:获取OS的一些信息

// 用来获取机器信息的
var os = require('os')

// 获取当前机器的 CPU 信息
console.log(os.cpus())
  • 出现了八个模块,代表是八核

在这里插入图片描述

  • 获取内存信息
var os = require('os')
// memory 内存
console.log(os.totalmem())

在这里插入图片描述
上面的单位是字节,除以1024,约等于8G内存大小

案例:获取用来操作路径的信息

// 用来操作路径的
 var path = require('path')
 
// 获取一个路径中的扩展名部分
 console.log(path.extname('c:/a/b/c/d/hello.txt'))

在这里插入图片描述

本文来自博客园,作者:兮动人,转载请注明原文链接:https://www.cnblogs.com/xdr630/p/15254833.html

原文地址:https://www.cnblogs.com/xdr630/p/15254833.html