以下是一个使用Node.js编写的程序,将“Hello World!”作为应用程序的所有路由的输出进行打印:
// 引入所需的模块
const http = require('http');
// 创建服务器
const server = http.createServer((req, res) => {
// 将“Hello World!”作为输出打印到控制台
console.log('Hello World!');
// 设置HTTP响应头
res.writeHead(200, {'Content-Type': 'text/plain'});
// 发送响应数据
res.end('Hello World!');
});
// 监听端口并启动服务器
const port = 3000;
server.listen(port, () => {
console.log(`Server running at http://localhost:${port}/`);
});
这个程序创建了一个HTTP服务器,当收到请求时,它会将“Hello World!”作为输出打印到控制台,并将相同的内容作为HTTP响应发送给客户端。你可以在浏览器中访问http://localhost:3000/
来查看输出。