Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行时环境,用于构建高性能的网络应用程序。它采用事件驱动、非阻塞 I/O 模型,使得在单线程上能够处理大量并发请求,非常适合构建实时、高并发的网络应用。
下面是一个简单的 Node.js 示例,演示了如何创建一个简单的 HTTP 服务器并监听指定端口:
// 引入 http 模块
const http = require('http');
// 创建 HTTP 服务器
const server = http.createServer((req, res) => {
// 设置响应头
res.writeHead(200, {'Content-Type': 'text/plain'});
// 发送响应数据
res.end('Hello, World!\n');
});
// 监听端口
const port = 3000;
server.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});