BNTang

服务端区分用户发送的是GET请求和POST请求

通过 HTTP 模块的 http.IncomingMessage 类的 .method 属性即可区别

具体源码如下所示

let http = require("http");

// 1.创建一个服务器实例对象
let server = http.createServer();
server.on("request", function (req, res) {
    console.log(req.method);
    res.writeHead(200, {
        "Content-Type": "text/plain; charset=utf-8"
    });
    if (req.method.toLowerCase() === "get") {
        res.end("利用GET请求的方式处理参数");
    } else if (req.method.toLowerCase() === "post") {
        res.end("利用POST请求的方式处理参数");
    }
});

// 2.指定监听的端口
server.listen(3000);

首先直接在浏览器中进行请求

然后在进行使用表单请求的方式效果如下所示

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HTTP模块-区分GET-POST请求</title>
</head>
<body>
<form action="http://127.0.0.1:3000/index.html" method="post">
    <input type="text" name="userName">
    <input type="text" name="password">
    <input type="submit" value="提交">
</form>
</body>
</html>

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-29
  • 2021-10-03
  • 2021-10-03
猜你喜欢
  • 2022-12-23
  • 2022-02-27
  • 2021-09-12
  • 2022-01-21
  • 2021-09-09
  • 2021-08-31
  • 2022-01-24
相关资源
相似解决方案