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

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

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

1. 纯文本响应

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

javascript 复制代码
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`:

javascript 复制代码
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`:

javascript 复制代码
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`:

javascript 复制代码
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`:

javascript 复制代码
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");

});
相关推荐
Solar202515 分钟前
构建高可靠性的机械设备企业数据采集系统:架构设计与实践指南
java·大数据·运维·服务器·架构
嫂子的姐夫18 分钟前
013-webpack:新东方
爬虫·python·webpack·node.js·逆向
地球资源数据云24 分钟前
MODIS(MCD19A2)中国2000-2024年度平均气溶胶光学深度数据集
大数据·服务器·数据库·人工智能·均值算法
_pengliang25 分钟前
react native ios 2个modal第二个不显示
javascript·react native·react.js
纳兰瑞雪26 分钟前
nodeJs electron程式开发demo
开发语言·前端·javascript
why技术27 分钟前
可怕,看到一个冷血的算法。人心逐利,算法只会更聪明地逐利。
前端·后端·算法
坚持学习前端日记30 分钟前
Nginx 搭建文件服务器
运维·服务器·nginx
未来之窗软件服务31 分钟前
服务器运维(二十二) 服务器安全探针封装—东方仙盟练气期
运维·云计算·仙盟创梦ide·东方仙盟·服务器探针
小北方城市网32 分钟前
第 4 课:微服务 API 网关设计与接口全生命周期管理|统一入口与接口治理实战
java·大数据·运维·人工智能·python·深度学习·数据库架构
wj3193233 分钟前
ping一个ip打印无法访问目的主机一次,然后打印请求超时问题定位过程
服务器·网络·嵌入式硬件·网络协议·tcp/ip·局域网网内