Node.js 服务器代码:如何创建简单的 HTTP 服务器并返回 'Hello World'
以下是一个简单的 Node.js 服务器代码,用于响应客户端的 HTTP 请求并返回'Hello World'消息:
const http = require('http');
http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World
');
}).listen(3000);
console.log('Server running at http://localhost:3000/');
该服务器监听来自本地主机的端口 3000 上的请求,并在收到请求时返回'Hello World'消息。在控制台中输出了服务器的运行地址。
原文地址: https://www.cveoy.top/t/topic/mFpa 著作权归作者所有。请勿转载和采集!