You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Microsoft Edge无法显示登录页,localhost:3000/login连接失败求助

问题分析与修复方案

核心问题:你的代码仅处理了POST方法的/login接口,但浏览器直接访问localhost:3000/login时发送的是GET请求,服务器没有对该请求做任何响应处理,导致浏览器无法获取内容;而Postman使用POST请求调用接口,所以能正常工作。

另外代码还有两处小问题:

  • body变量未声明,会成为全局变量,存在潜在风险
  • 错误处理回调里的console.log(error)引用了未定义的error变量,应该用参数err

以下是修复后的代码:

const http = require('http');

const server = http.createServer((req, res) => {
    // 声明body变量,避免全局污染
    let body = [];
    
    req.on('error', (err) => {
        // 修正变量名
        console.log(err);
    }).on("data", (chunkdata) => {
        body.push(chunkdata);
        console.log(chunkdata.toString());
    }).on("end", () => {
        body = Buffer.concat(body).toString();
        
        // 处理POST请求的/login接口
        if (req.method === "POST" && req.url === "/login") {
            console.log(body);
            try {
                const user = JSON.parse(body);
                const { username, password } = user;
                res.statusCode = 200;
                res.setHeader("Content-Type", "application/json");
                // 返回标准JSON格式内容
                res.end(JSON.stringify({
                    message: `Your username is ${username} and password is ${password}`
                }));
            } catch (parseErr) {
                // 处理JSON解析错误
                res.statusCode = 400;
                res.setHeader("Content-Type", "application/json");
                res.end(JSON.stringify({ error: "Invalid JSON format" }));
            }
        } 
        // 处理GET请求的/login,返回登录表单
        else if (req.method === "GET" && req.url === "/login") {
            res.statusCode = 200;
            res.setHeader("Content-Type", "text/html");
            res.end(`
                <!DOCTYPE html>
                <html>
                <body>
                    <h1>Login</h1>
                    <form method="POST" action="/login">
                        <div>
                            <label>Username:</label>
                            <input type="text" name="username">
                        </div>
                        <div>
                            <label>Password:</label>
                            <input type="password" name="password">
                        </div>
                        <button type="submit">Submit</button>
                    </form>
                </body>
                </html>
            `);
        }
        // 处理其他未匹配的请求
        else {
            res.statusCode = 404;
            res.setHeader("Content-Type", "text/plain");
            res.end("404 Not Found");
        }
    });
})

server.listen(3000, () => { 
  console.log("Server running on http://localhost:3000");
});

修复说明:

  • 添加了GET请求/login的处理,返回一个简单的登录表单,这样Edge访问时能正常显示页面
  • 修复了变量未声明和错误处理的变量名问题
  • 优化了POST请求的响应,返回标准JSON格式,并添加了JSON解析错误的处理
  • 增加了404响应,处理其他未匹配的请求

内容的提问来源于stack exchange,提问作者tmang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 05:01:34