// 引入 http 模块
const http = require('http');
// 设置服务器端口
const PORT = 38681;
// 创建服务器
const server = http.createServer((req, res) => {
// 检查请求方法是否为 GET,路径是否为 "/myurl"
if (req.method === 'GET' && req.url === '/xxx') {
// 设置响应头
res.writeHead(200, { 'Content-Type': 'text/html; charset=UTF-8' });
// 返回指定的 HTML 内容
res.end(`<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>0</title>
</head>
<body>
<a href="https://xxx.xxx.com">超链接</a>
</body>
</html>`);
} else {
// 其他路径返回 400 状态码
res.writeHead(400, { 'Content-Type': 'text/plain; charset=UTF-8' });
res.end('400 Bad Request');
}
});
// 启动服务器
server.listen(PORT,'0.0.0.0', () => {
console.log(`服务器运行在 http://localhost:${PORT}`);
});
node开启http服务
于 2025-02-10 12:59:05 首次发布