Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境。它让开发者可以使用 JavaScript 来编写服务器端代码,从而构建快速、可扩展的网络应用。
安装 Node.js
在开始之前,你需要确保你的系统上安装了 Node.js。你可以通过以下链接查看安装指南:
快速示例
以下是一个简单的 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('服务器运行在 http://localhost:3000/');
});
代码结构
Node.js 代码通常由模块组成。你可以通过 require()
函数来导入模块。
const math = require('./math');
console.log(math.add(1, 2)); // 输出 3
资源
Node.js Logo