node 基础

使用 vscore 写 node 的配置文件

{
    "version": "0.2.0",
    "configurations": [
    {
            "type": "pwa-node",
            "request": "launch",
            "name": "Node",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "program": "${workspaceFolder}/main.js",
            "args": [
                "-xxxxxx=xxxxxx",
                "-xxxxxx=xxxxxx"
            ],
        },
        {
            "name": "Launch_Go",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "program": "${workspaceFolder}/admin/build/main.go",
            "env": {
                "CONSUL_IP": "127.0.0.1:8500"
            }
            // "args": ["--conf", "${workspaceFolder}/admin/build/config.yaml"]
            // "args": ["--svr", "display"]
        }
    ]
}

node 特点

  1. 事件驱动
  2. 非阻塞 IO 模式(异步)
  3. 轻量和高效

模块的导出与导入

模块的导出

class User {
    constructor() {
        this.userName = "小明"
        this.passWord = "123456"
    }
}

module.exports = User

模块的导入

const User = require("./user")
let user1=new User()
console.log(user1)

文件的读取

const fs = require("fs")

// 同步
const content = fs.readFileSync('hellow.txt', { flag: "r", encoding: "utf-8" })
console.log(content.toString())

console.log("*************************")

// 异步
fs.readFile("hellow.txt", { flag: "r", encoding: "utf-8" }, function (err, data) {
    if (err) {
        console.log(err)
    } else {
        console.log(data)
    }
})

获取系统的相关信息

const path = require('path')
let extname = path.extname("./t.txt")
console.log(extname)

// 获取当前执行文件的目录
console.log(__dirname)

// 获取当前的执行文件
console.log(__filename)
console.log(path.extname(__filename))


//对路径进行解析
console.log(path.parse(__filename))


const os = require('os')

//cup 的相关信息
console.log(os.cpus())

//内存相关信息
console.log(os.totalmem())

//cpu 的架构
console.log(os.arch())

//闲置内存
console.log(os.freemem())

//主机名称
console.log(os.hostname())
console.log(os.platform())
posted @ 2022-06-20 15:20  沧海一声笑rush  阅读(23)  评论(0编辑  收藏  举报