Node.js 基本内置模块及其作用(8个)-fs (File System)模块、path模块、http模块、events模块、os模块、url模块、crypto模块、querystring模块
warning:
这篇文章距离上次修改已过191天,其中的内容可能已经有所变动。
在Node.js中,有许多内置的模块可以使用,以下是其中的一些:
- fs (File System)模块:这是Node.js中的一个核心模块,用于实现文件的读写操作。
const fs = require('fs');
fs.readFile('./example.txt', 'utf8', (err, data) => {
if (err) throw err;
console.log(data);
});
- path模块:这个模块用于处理文件路径。
const path = require('path');
console.log(path.join('/foo', 'bar', 'baz/asdf', 'quux', '..'));
// 输出: '/foo/bar/baz/asdf'
- http模块:这是Node.js中的一个核心模块,用于实现HTTP服务器和客户端功能。
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000/');
});
以上代码分别展示了如何使用fs、path和http模块。每个模块都有其特定的功能,可以根据需要进行使用。
评论已关闭