当前位置: 首页 > article >正文

Node 处理客户端不同的请求方法

一、使用 `http` 模块处理请求方法

1. 创建 HTTP 服务器

const http = require("http");

const server = http.createServer((req, res) => {

  // 处理不同的请求方法

  switch (req.method) {

    case "GET":

      handleGetRequest(req, res);

      break;

    case "POST":

      handlePostRequest(req, res);

      break;

    case "PUT":

      handlePutRequest(req, res);

      break;

    case "DELETE":

      handleDeleteRequest(req, res);

      break;

    default:

      handleUnsupportedMethod(req, res);

  }

});

// 监听端口

server.listen(3000, () => {

  console.log("Server running on port 3000");

});

2. 处理 GET 请求

function handleGetRequest(req, res) {

  // 设置响应头

  res.writeHead(200, { "Content-Type": "text/plain" });

  // 发送响应内容

  res.end("This is a GET request");

}

3. 处理 POST 请求

function handlePostRequest(req, res) {

  let body = "";

  // 接收请求体数据

  req.on("data", (chunk) => {

    body += chunk.toString();

  });

  req.on("end", () => {

    // 设置响应头

    res.writeHead(200, { "Content-Type": "text/plain" });

    // 发送响应内容

    res.end(`This is a POST request with body: ${body}`);

  });

}

4. 处理 PUT 请求

function handlePutRequest(req, res) {

  let body = "";

  req.on("data", (chunk) => {

    body += chunk.toString();

  });

  req.on("end", () => {

    // 设置响应头

    res.writeHead(200, { "Content-Type": "text/plain" });

    // 发送响应内容

    res.end(`This is a PUT request with body: ${body}`);

  });

}

5. 处理 DELETE 请求

function handleDeleteRequest(req, res) {

  // 设置响应头

  res.writeHead(200, { "Content-Type": "text/plain" });

  // 发送响应内容

  res.end("This is a DELETE request");

}

6. 处理不支持的请求方法

function handleUnsupportedMethod(req, res) {

  // 设置响应头

  res.writeHead(405, { "Content-Type": "text/plain" });

  // 发送响应内容

  res.end("Method Not Allowed");

}

二、使用 `express` 框架处理请求方法

1. 安装 `express` 框架

npm install express

2. 创建 `express` 服务器

const express = require("express");

const app = express();

// 处理 GET 请求

app.get("/", (req, res) => {

  res.send("This is a GET request");

});

// 处理 POST 请求

app.post("/", (req, res) => {

  res.send("This is a POST request");

});

// 处理 PUT 请求

app.put("/", (req, res) => {

  res.send("This is a PUT request");

});

// 处理 DELETE 请求

app.delete("/", (req, res) => {

  res.send("This is a DELETE request");

});

// 监听端口

app.listen(3000, () => {

  console.log("Server running on port 3000");

});


http://www.kler.cn/a/530687.html

相关文章:

  • 新到手路由器宽带上网设置八步法
  • Visual Studio Code应用本地部署的deepseek
  • 【Numpy核心编程攻略:Python数据处理、分析详解与科学计算】2.3 结构化索引:记录数组与字段访问
  • 9 点结构模块(point.rs)
  • WebForms SortedList 深度解析
  • C++并发编程指南08
  • DeepSeek 原理解析:与主流大模型的差异及低算力优势
  • 【基于SprintBoot+Mybatis+Mysql】电脑商城项目之用户登录
  • 【数据结构与算法】力扣 5. 最长回文子串
  • [ VS Code 插件开发 ] 使用 Task ( 任务 ) 代替 createTerminal (终端) 来执行命令
  • 数据库和数据表的创建、修改、与删除
  • 冷启动+强化学习:DeepSeek-R1 的原理详解——无需监督数据的推理能力进化之路
  • 基于vue船运物流管理系统设计与实现(源码+数据库+文档)
  • 蓝桥杯学习笔记01
  • 【Qt】常用的容器
  • llama.cpp GGUF 模型格式
  • GWO优化SVM回归预测matlab
  • Mac怎么彻底卸载软件,简单彻底的卸载方式
  • 【数据结构-Trie树】力扣677. 键值映射
  • SQL/Panda映射关系
  • Spring Boot 2 快速教程:WebFlux处理流程(五)
  • 自制虚拟机(C/C++)(三、做成标准GUI Windows软件,扩展指令集,直接支持img软盘)
  • 轮转数组-三次逆置
  • Chromium132 编译指南 - Android 篇(六):从 Linux 版切换到 Android 版
  • 鸢尾花书《编程不难》02---学习书本里面的三个案例
  • 使用VCS进行单步调试的步骤