Node.js os module All In One

Posted xgqfrms

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Node.js os module All In One相关的知识,希望对你有一定的参考价值。

Node.js os module All In One

Node.js os module All In One

OS Node.js v12.4.0

// ESM
import os from \'node:os\';

// CJS
const os = require(\'node:os\');

https://nodejs.org/api/os.html

demos

ESM .mjs / package.json "type": "module" + .js

// write your code here
import os from \'node:os\'
// const os = require(\'node:os\');

// console.log(`os =`, os)
// console.log(`os.arch =`, os.arch)
/* os.arch = [Function: arch] 
  [Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
 */
console.log(`os.arch() =`, os.arch())
// console.log(`os.cpus() =`, os.cpus())
console.log(`os.cpus().length =`, os.cpus().length)
console.log(`os.cpus()[0] =`, os.cpus()[0])
console.log(`os.hostname() =`, os.hostname())
console.log(`os.platform() =`, os.platform())
console.log(`os.userInfo() =`, os.userInfo())
console.log(`os.version() =`, os.version())
// console.log(`os =`, os)

/*

os = 
  arch: [Function: arch] 
    [Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
  ,
  cpus: [Function: cpus],
  endianness: [Function: endianness] 
    [Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
  ,
  freemem: [Function: getFreeMem] 
    [Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
  ,
  getPriority: [Function: getPriority],
  homedir: [Function: __node_internal_checkError] 
    [Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
  ,
  hostname: [Function: __node_internal_checkError] 
    [Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
  ,
  loadavg: [Function: loadavg],
  networkInterfaces: [Function: networkInterfaces],
  platform: [Function: platform] 
    [Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
  ,
  release: [Function: getOSRelease] 
    [Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
  ,
  setPriority: [Function: setPriority],
  tmpdir: [Function: tmpdir] 
    [Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
  ,
  totalmem: [Function: getTotalMem] 
    [Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
  ,
  type: [Function: getOSType] 
    [Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
  ,
  userInfo: [Function: userInfo],
  uptime: [Function: getUptime] 
    [Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
  ,
  version: [Function: getOSVersion] 
    [Symbol(Symbol.toPrimitive)]: [Function (anonymous)]
  ,
  constants: [Object: null prototype] 
    UV_UDP_REUSEADDR: 4,
    dlopen: [Object: null prototype] 
      RTLD_LAZY: 1,
      RTLD_NOW: 2,
      RTLD_GLOBAL: 8,
      RTLD_LOCAL: 4
    ,
    errno: [Object: null prototype] 
      E2BIG: 7,
      EACCES: 13,
      EADDRINUSE: 48,
      EADDRNOTAVAIL: 49,
      EAFNOSUPPORT: 47,
      EAGAIN: 35,
      EALREADY: 37,
      EBADF: 9,
      EBADMSG: 94,
      EBUSY: 16,
      ECANCELED: 89,
      ECHILD: 10,
      ECONNABORTED: 53,
      ECONNREFUSED: 61,
      ECONNRESET: 54,
      EDEADLK: 11,
      EDESTADDRREQ: 39,
      EDOM: 33,
      EDQUOT: 69,
      EEXIST: 17,
      EFAULT: 14,
      EFBIG: 27,
      EHOSTUNREACH: 65,
      EIDRM: 90,
      EILSEQ: 92,
      EINPROGRESS: 36,
      EINTR: 4,
      EINVAL: 22,
      EIO: 5,
      EISCONN: 56,
      EISDIR: 21,
      ELOOP: 62,
      EMFILE: 24,
      EMLINK: 31,
      EMSGSIZE: 40,
      EMULTIHOP: 95,
      ENAMETOOLONG: 63,
      ENETDOWN: 50,
      ENETRESET: 52,
      ENETUNREACH: 51,
      ENFILE: 23,
      ENOBUFS: 55,
      ENODATA: 96,
      ENODEV: 19,
      ENOENT: 2,
      ENOEXEC: 8,
      ENOLCK: 77,
      ENOLINK: 97,
      ENOMEM: 12,
      ENOMSG: 91,
      ENOPROTOOPT: 42,
      ENOSPC: 28,
      ENOSR: 98,
      ENOSTR: 99,
      ENOSYS: 78,
      ENOTCONN: 57,
      ENOTDIR: 20,
      ENOTEMPTY: 66,
      ENOTSOCK: 38,
      ENOTSUP: 45,
      ENOTTY: 25,
      ENXIO: 6,
      EOPNOTSUPP: 102,
      EOVERFLOW: 84,
      EPERM: 1,
      EPIPE: 32,
      EPROTO: 100,
      EPROTONOSUPPORT: 43,
      EPROTOTYPE: 41,
      ERANGE: 34,
      EROFS: 30,
      ESPIPE: 29,
      ESRCH: 3,
      ESTALE: 70,
      ETIME: 101,
      ETIMEDOUT: 60,
      ETXTBSY: 26,
      EWOULDBLOCK: 35,
      EXDEV: 18
    ,
    signals: [Object: null prototype] 
      SIGHUP: 1,
      SIGINT: 2,
      SIGQUIT: 3,
      SIGILL: 4,
      SIGTRAP: 5,
      SIGABRT: 6,
      SIGIOT: 6,
      SIGBUS: 10,
      SIGFPE: 8,
      SIGKILL: 9,
      SIGUSR1: 30,
      SIGSEGV: 11,
      SIGUSR2: 31,
      SIGPIPE: 13,
      SIGALRM: 14,
      SIGTERM: 15,
      SIGCHLD: 20,
      SIGCONT: 19,
      SIGSTOP: 17,
      SIGTSTP: 18,
      SIGTTIN: 21,
      SIGTTOU: 22,
      SIGURG: 16,
      SIGXCPU: 24,
      SIGXFSZ: 25,
      SIGVTALRM: 26,
      SIGPROF: 27,
      SIGWINCH: 28,
      SIGIO: 23,
      SIGINFO: 29,
      SIGSYS: 12
    ,
    priority: [Object: null prototype] 
      PRIORITY_LOW: 19,
      PRIORITY_BELOW_NORMAL: 10,
      PRIORITY_NORMAL: 0,
      PRIORITY_ABOVE_NORMAL: -7,
      PRIORITY_HIGH: -14,
      PRIORITY_HIGHEST: -20
    
  ,
  EOL: \'\\n\',
  devNull: \'/dev/null\'



*/

$ node ./os-module.mjs
os.arch() = x64
os.cpus().length = 12
os.cpus()[0] = 
  model: \'Intel(R) Core(TM) i7-8750H CPU @ 2.20GHz\',
  speed: 2200,
  times:  user: 3276490, nice: 0, sys: 2014940, idle: 30180650, irq: 0 

os.hostname() = xgqfrms-mbp.local
os.platform() = darwin
os.userInfo() = 
  uid: 501,
  gid: 20,
  username: \'xgqfrms-mbp\',
  homedir: \'/Users/xgqfrms-mbp\',
  shell: \'/bin/zsh\'

os.version() = Darwin Kernel Version 21.4.0: Mon Feb 21 20:34:37 PST 2022; root:xnu-8020.101.4~2/RELEASE_X86_64


CJS .js



(

一个简单的node.js

var express = require(‘./node_modules/express‘)
var app = express()
// 设置跨域访问
app.all(‘*‘, function(req, res, next) {
res.header(‘Access-Control-Allow-Origin‘, ‘*‘)
res.header(‘Access-Control-Allow-Headers‘, ‘X-Requested-With‘)
res.header(‘Access-Control-Allow-Methods‘, ‘PUT,POST,GET,DELETE,OPTIONS‘)
res.header(‘Content-Type‘, ‘application/json;charset=utf-8‘)
next()
})
var data = [
{ name: ‘lisi‘, age: 20 },
{ name: ‘wangwu‘, age: 30 }
]
// 写个接口getPersonal
app.get(‘/getUser‘, function(req, res) {
res.status(200),
res.json(data)
})
// 配置服务端口
var server = app.listen(4000, function() {
var host = server.address().address
var post = server.address().post
console.log(‘启动成功!!!‘)
})

以上是关于Node.js os module All In One的主要内容,如果未能解决你的问题,请参考以下文章

python之OS.module;building_in_module

Python relative import local package module file All In One

一个简单的node.js

一个简单的node.js

JavaScript ES modules import and export with trailing commas All In One

Node.js node_modules 相互干扰