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

Node 服务器数据响应类型处理

一、设置不同的响应数据类型

在 Node.js 的 `http` 模块中,通过 `res.writeHead` 方法可以设置不同的响应头,以指定响应的数据类型。

1. 纯文本响应

对于纯文本响应,可以将 `Content-Type` 设置为 `text/plain`

const http = require("http");

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

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

  res.end("你好");

});

server.listen(3000, () => {

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

});

2. HTML 响应

对于 HTML 响应,可以将 `Content-Type` 设置为 `text/html`:

const http = require("http");

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

  res.writeHead(200, { "Content-Type": "text/html;charset=utf8;" });

  res.end("<h1>你好<h1>");

});

server.listen(3000, () => {

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

});

3. JSON 响应

对于 JSON 响应,可以将 `Content-Type` 设置为 `application/json`:

const http = require("http");

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

  const data = { message: "Hello, World!" };

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

  res.end(JSON.stringify(data));

});

server.listen(3000, () => {

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

});

4. 图片响应(以 JPEG 为例)

对于图片响应,需要将 `Content-Type` 设置为相应的图片类型,如 `image/jpeg`:

const http = require("http");

const fs = require("fs");

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

  const imagePath = "path/to/your/image.jpg";

  fs.readFile(imagePath, (err, data) => {

    if (err) {

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

      res.end("Error loading image");

    } else {

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

      res.end(data);

    }

  });

});

server.listen(3000, () => {

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

});

5. 二进制文件响应

对于二进制文件响应,可以根据文件类型设置相应的 `Content-Type`:

const http = require("http");

const fs = require("fs");

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

  const binaryFilePath = "path/to/your/binaryfile.bin";

  fs.readFile(binaryFilePath, (err, data) => {

    if (err) {

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

      res.end("Error loading binary file");

    } else {

      res.writeHead(200, { "Content-Type": "application/octet-stream" });

      res.end(data);

    }

  });

});

server.listen(3000, () => {

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

});


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

相关文章:

  • 交叉验证、精确率、召回率
  • C++:结构体和类
  • [250203] glibc 2.41 发布 | Flutter 颜色管理库 color_palette_plus 2.0.0 发布
  • 全栈开发:使用.NET Core WebAPI构建前后端分离的核心技巧(一)
  • Windows图形界面(GUI)-QT-C/C++ - QT Stacked Widget
  • 数据结构之栈和队列(超详解)
  • SLAM技术栈 ——《视觉SLAM十四讲》学习笔记(一)
  • c++ stl 遍历算法和查找算法
  • BMS和无刷电机产品拆解学习
  • TryHackMe: TryPwnMe Two
  • 使用递归解决编程题
  • 编程AI深度实战:AI编程工具哪个好? Copilot vs Cursor vs Cody vs Supermaven vs Aider
  • redis教程
  • 《苍穹外卖》项目学习记录-Day11销量排名统计
  • JavaScript系列(55)--安全编程实践详解
  • 代码随想录二刷|二叉树7
  • Leetcode 3440. Reschedule Meetings for Maximum Free Time II
  • 刷题汇总一览
  • 在Vue3项目中使用百度地图
  • vscode flutter 项目连接 mumu 浏览器
  • BUUCTF Pwn axb_2019_brop64 题解
  • aws(学习笔记第二十七课) 使用aws API Gateway+lambda体验REST API
  • C++泛型编程指南07 函数重载
  • 来自谷歌新作:SFT负责记忆遵循,RL驱动泛化迁移?
  • Use-DeepSeek增效
  • 将D盘空间划分给C盘