NodeJS常用模块介绍
封面图
•
2025-10-19
•
nodejs
256
0
0
0
在 Node.js 中,模块是构建应用程序的重要组成部分。Node.js 提供了许多内置的常用模块(也称为核心模块),它们为开发者提供了处理文件系统、网络通信、数据加密、路径操作等功能的能力。以下是一些 **Node.js 常用模块** 的介绍: --- ## 📦 一、常用的内置模块(Core Modules) ### 1. `fs` - 文件系统模块 用于读取、写入、删除和操作文件。 ```js const fs = require('fs'); // 同步读取文件 const data = fs.readFileSync('file.txt', 'utf8'); console.log(data); // 异步读取文件 fs.readFile('file.txt', 'utf8', (err, data) => { if (err) throw err; console.log(data); }); ``` ### 2. `path` - 路径处理模块 用于处理和转换文件路径。 ```js const path = require('path'); console.log(path.join(__dirname, 'folder', 'file.txt')); // 拼接路径 console.log(path.basename('/user/test/file.txt')); // 输出 "file.txt" ``` ### 3. `http` - HTTP 服务器模块 创建 HTTP 服务器。 ```js const http = require('http'); http.createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'text/plain' }); res.end('Hello World\n'); }).listen(3000, () => { console.log('Server running at http://localhost:3000/'); }); ``` ### 4. `https` - HTTPS 服务器模块 与 `http` 类似,但支持 HTTPS 协议。 ### 5. `events` - 事件模块 用于实现事件驱动编程。 ```js const EventEmitter = require('events'); class MyEmitter extends EventEmitter {} const myEmitter = new MyEmitter(); myEmitter.on('event', () => { console.log('an event occurred!'); }); myEmitter.emit('event'); ``` ### 6. `os` - 操作系统信息模块 获取操作系统相关的信息。 ```js const os = require('os'); console.log('OS platform:', os.platform()); console.log('CPU architecture:', os.arch()); console.log('Free memory:', os.freemem() / 1024 / 1024, 'MB'); ``` ### 7. `process` - 进程对象 提供关于当前 Node.js 进程的信息。 ```js console.log('Current process PID:', process.pid); console.log('Environment variables:', process.env); ``` ### 8. `util` - 工具函数模块 包含一些实用工具函数,如 `format()`、`inherits()` 等。 ```js const util = require('util'); function Person(name) { this.name = name; } Person.prototype.greet = function() { console.log(`Hello, ${this.name}`); }; const person = new Person('Alice'); util.inherits(Person, EventEmitter); // 继承 EventEmitter ``` ### 9. `child_process` - 子进程模块 用于创建子进程并与其进行交互。 ```js const { exec } = require('child_process'); exec('ls -la', (err, stdout, stderr) => { if (err) { console.error(err); return; } console.log(stdout); }); ``` ### 10. `crypto` - 加密模块 提供加密功能,如哈希、加密、解密等。 ```js const crypto = require('crypto'); const hash = crypto.createHash('sha256'); hash.update('Hello, world!'); console.log(hash.digest('hex')); ``` --- ## 🧩 二、第三方常用模块(推荐) 虽然这些不是 Node.js 内置模块,但在开发中非常常见: | 模块名 | 功能说明 | |----------------|----------------------------------| | `express` | Web 应用框架 | | `mongoose` | MongoDB 对象模型映射器 | | `axios` | HTTP 请求库 | | `dotenv` | 从 `.env` 文件加载环境变量 | | `winston` | 日志记录库 | | `multer` | 处理文件上传 | | `jsonwebtoken` | JWT(JSON Web Token)生成和验证 | | `moment` | 日期时间处理 | --- ## ✅ 总结 | 模块名 | 功能 | 是否内置 | |--------|------|----------| | `fs` | 文件系统操作 | ✅ | | `path` | 路径处理 | ✅ | | `http` | 创建 HTTP 服务器 | ✅ | | `events` | 事件处理 | ✅ | | `os` | 获取系统信息 | ✅ | | `process` | 进程信息 | ✅ | | `util` | 工具函数 | ✅ | | `crypto` | 加密功能 | ✅ | | `child_process` | 子进程管理 | ✅ | --- 如果你有特定的应用场景(比如 Web 开发、文件处理、API 接口等),我可以为你推荐更具体的模块组合或示例代码 😊
上一篇:
什么是 Node.js
下一篇:
js中如何判断一个层是否隐藏 判断的JS语句怎么写,我是想写一个方法,判断一个层是隐藏还是显示,隐藏
标题录入,一次不能超过6条
返回顶部
留言
留言
评论