Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行时环境。它让开发者可以在服务器端运行 JavaScript 代码,从而构建快速、可扩展的网络应用程序。

特点

  • 事件驱动、非阻塞 I/O: Node.js 使用单线程模型,通过事件循环机制实现非阻塞 I/O 操作,从而提高应用程序的并发性能。
  • 模块化: Node.js 提供了丰富的模块化机制,方便开发者组织代码。
  • 跨平台: Node.js 支持多种操作系统,包括 Windows、Linux 和 macOS。

示例

以下是一个简单的 Node.js 服务器示例:

const http = require('http');

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello, Node.js!');
});

server.listen(3000, () => {
  console.log('Server running at http://localhost:3000/');
});

资源

Node.js Logo