Node.js 是基于事件驱动的、非阻塞 I/O 模型的平台,利用事件循环机制和回调函数可以有效处理大量并发请求。
const http = require('http');
// 创建一个 HTTP 服务器
const server = http.createServer((req, res) => {
// 模拟异步的 I/O 操作,比如数据库查询或文件读取
setTimeout(() => {
res.writeHead(200);
res.end('Hello World\n');
}, 100);
});
// 监听端口
server.listen(8000, () => {
console.log('Server is running at http://localhost:8000/');
});