Node.js 提供了一个丰富的 API,用于创建高效的服务器和网络应用程序。以下是一些常用的 Node.js API 功能:

模块

Node.js 使用 CommonJS 模块系统。你可以通过 require 关键字导入模块。

  • 使用 fs 模块进行文件系统操作。
  • 使用 http 模块创建 HTTP 服务器。

文件系统 (fs)

fs 模块提供了文件读写、文件系统路径操作等功能。

const fs = require('fs');

fs.readFile('example.txt', 'utf8', (err, data) => {
  if (err) {
    return console.error(err);
  }
  console.log(data);
});

HTTP 服务器

使用 http 模块可以创建一个简单的 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(8000, () => {
  console.log('Server running at http://localhost:8000/');
});

图片示例

Node.js Logo

更多关于 Node.js 的信息,请访问我们的Node.js 教程

抱歉,您的请求不符合要求