Skip to content

LydiaAgute/node_test_server

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Node.js 测试服务器

一个用于设备端请求测试的 Node.js 服务器,支持详细的请求日志记录,帮助定位请求问题。

功能特性

  • 详细请求日志 - 记录所有 HTTP 请求的完整信息,包括请求头、请求体、响应信息
  • multipart/form-data 解析 - 详细解析 multipart 数据,帮助定位上传问题
  • 文件上传测试 - 支持多种上传方式(Multer、流式上传)
  • 文件下载测试 - 支持断点续传、限速下载
  • WebSocket 测试 - 基本 WebSocket 连接测试
  • 各种 HTTP 方法测试 - GET、POST、PUT、PATCH 等
  • 特殊场景测试 - 长阻塞响应、重定向、延迟响应等

快速开始

安装依赖

npm install

启动服务器

npm start

服务器将在 http://localhost:3000 启动。

使用 Docker

docker-compose up -d

API 接口文档

基础测试接口 (/test-*)

GET 请求测试

接口 方法 描述
/test-get GET 测试 GET 请求,返回查询参数
/test-redirect GET 测试重定向(302),重定向到 /test-get
/test-user GET 返回用户对象 {id: 1, name: "John Doe"}
/test-delay GET 延迟响应,参数 delay 指定延迟毫秒数(默认 10000ms)
/long-response GET 阻塞 10 秒后响应
/no-response GET 阻塞 10 秒后无响应(模拟超时)

示例请求:

# GET 请求测试
curl "http://localhost:3000/test-get?param1=value1&param2=value2"

# 延迟响应测试(延迟 5 秒)
curl "http://localhost:3000/test-delay?delay=5000"

# 用户信息测试
curl "http://localhost:3000/test-user"

POST 请求测试

接口 方法 描述
/test-post POST 测试 POST 请求,返回请求体
/test-user POST 测试用户创建,body 为 {name: "Alice"} 时返回成功

示例请求:

# POST JSON 数据
curl -X POST "http://localhost:3000/test-post" \
  -H "Content-Type: application/json" \
  -d '{"key": "value"}'

# POST 表单数据
curl -X POST "http://localhost:3000/test-post" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "key=value"

# 创建用户
curl -X POST "http://localhost:3000/test-user" \
  -H "Content-Type: application/json" \
  -d '{"name": "Alice"}'

PUT/PATCH 请求测试

接口 方法 描述
/test-put PUT 测试 PUT 请求,返回请求体
/test-patch PATCH 测试 PATCH 请求,返回请求体

示例请求:

# PUT 请求
curl -X PUT "http://localhost:3000/test-put" \
  -H "Content-Type: application/json" \
  -d '{"key": "updated"}'

# PATCH 请求
curl -X PATCH "http://localhost:3000/test-patch" \
  -H "Content-Type: application/json" \
  -d '{"key": "partial_update"}'

文件上传接口 (/upload*)

Multer 文件上传(multipart/form-data)

这些接口使用 Multer 中间件,只支持 multipart/form-data 格式

接口 方法 Content-Type 描述
/upload POST multipart/form-data 单文件上传,字段名必须为 file,支持附加表单字段
/upload-multiple POST multipart/form-data 多文件上传,字段名 files,最多 10 个
/upload-any POST multipart/form-data 单文件上传,任意字段名

重要说明:

  • /upload 接口可以同时接收文件和非文件字段(如 id、type、grade 等)
  • 文件字段名必须为 file(除非使用 /upload-any
  • 非文件字段会出现在 req.body 中,并在日志中详细打印

示例请求:

# 单文件上传(字段名必须为 file)
curl -X POST "http://localhost:3000/upload" \
  -F "file=@/path/to/your/file.txt"

# 带附加表单字段的上传(id、type、grade 等)
curl -X POST "http://localhost:3000/upload" \
  -F "file=@/path/to/file.bin" \
  -F "id=12345" \
  -F "type=document" \
  -F "grade=A"

# 多文件上传
curl -X POST "http://localhost:3000/upload-multiple" \
  -F "files=@/path/to/file1.txt" \
  -F "files=@/path/to/file2.txt"

流式文件上传(application/octet-stream)

这些接口支持纯二进制流上传,不使用 multipart 格式,直接将请求体保存为文件。

接口 方法 Content-Type 描述
/stream-upload POST/PUT/PATCH application/octet-stream 或任意 流式上传,请求头 x-filename 指定文件名

重要说明:

  • 不支持 multipart/form-data,请求体直接作为文件内容
  • 必须在请求头中提供 x-filename 指定保存的文件名
  • 适合上传纯二进制数据或大文件

示例请求:

# 流式上传(需要在请求头指定文件名)
curl -X POST "http://localhost:3000/stream-upload" \
  -H "x-filename: uploaded-file.bin" \
  -H "Content-Type: application/octet-stream" \
  --data-binary @/path/to/your/file.bin

接口对比

特性 /upload (Multer) /stream-upload (流式)
Content-Type multipart/form-data application/octet-stream
支持非文件字段 ✅ 是 ❌ 否
文件字段名要求 必须为 file 无(请求头指定文件名)
适合场景 表单+文件混合上传 纯二进制文件上传
日志详细程度 解析每个部分 显示接收进度

文件下载接口 (/download*)

接口 方法 描述
/download/:filename GET 下载指定文件,支持 Range 请求(断点续传)
/slow-download GET 限速下载测试

限速下载参数:

参数 类型 默认值 描述
speed int 1 每秒发送的字节数
fileSize int 10485760 文件大小(字节),默认 10MB

示例请求:

# 下载文件
curl "http://localhost:3000/download/test.txt" -o output.txt

# 断点续传(从 1000 字节开始)
curl "http://localhost:3000/download/test.txt" \
  -H "Range: bytes=1000-" -o output.txt

# 限速下载(每秒 100 字节,文件大小 1KB)
curl "http://localhost:3000/slow-download?speed=100&fileSize=1024" -o output.txt

WebSocket 接口

WebSocket 服务在同一个端口(3000)上运行。

连接地址: ws://localhost:3000

示例代码:

const ws = new WebSocket('ws://localhost:3000');

ws.onopen = () => {
  console.log('Connected');
  ws.send('Hello Server');
};

ws.onmessage = (event) => {
  console.log('Received:', event.data);
};

ws.onclose = () => {
  console.log('Disconnected');
};

也可以使用项目中的 test-ws.html 文件在浏览器中测试 WebSocket。


详细请求日志功能

服务器会自动记录所有请求的详细信息,帮助定位问题。

日志内容

请求信息

  • 📅 时间戳
  • 🌐 请求方法(GET/POST/PUT/PATCH 等)
  • 🔗 URL 和路径
  • 🔍 查询参数
  • 📋 完整请求头(敏感信息自动脱敏)
  • 📊 Content-Length 和 Content-Type

multipart/form-data 详细解析

  • Boundary 解析和验证
  • 原始数据预览(前 500 字节)
  • 每个部分的详细信息:
    • 字段名 (name)
    • 文件名 (filename)
    • 内容类型 (Content-Type)
    • 内容大小
    • 是否为二进制数据
    • 内容预览

响应信息

  • ⏱️ 请求耗时
  • 📊 状态码
  • 📊 响应头
  • 📝 响应体内容(如果小于 1KB)

错误信息

  • ❌ Multer 错误详细解释
  • ❌ 请求错误堆栈
  • ⚠️ 格式问题警告

日志示例

============================================================
  📥 收到请求 [abc123]
============================================================
📅 时间: 2026/04/09 09:55:00.123
🌐 方法: POST
🔗 URL: /upload
📍 路径: /upload
🔍 查询参数: {}

📋 请求头:
{
  "content-type": "multipart/form-data; boundary=----WebKitFormBoundary",
  "content-length": "1048576"
}

📦 检测到 multipart/form-data 请求
   Content-Type: multipart/form-data; boundary=----WebKitFormBoundary
   Content-Length: 1048576 (1.00 MB)
   Boundary: ----WebKitFormBoundary

📥 接收进度: 100.0% (1.00 MB/1.00 MB)
✅ 数据接收完成: 1.00 MB

📦 共解析到 2 个部分:
   部分 #1:
   字段名 (name): file
   文件名 (filename): test.bin
   内容类型: application/octet-stream
   内容大小: 1.00 MB
   是否二进制: 是

   部分 #2:
   字段名 (name): description
   内容大小: 50 Bytes
   是否二进制: 否
   内容预览: This is a test file

问题诊断提示

当请求出现问题时,日志会提供诊断提示:

⚠️ 警告: 无法解析 boundary,请求头可能格式错误
   请检查 Content-Type 是否包含 boundary 参数
   正确格式示例: multipart/form-data; boundary=----WebKitFormBoundaryXXXXX

⚠️ 数据未以正确的 boundary 开始
   期望开头: ----WebKitFormBoundary...
   实际开头: (实际数据开头)...

❌ Multer 错误: LIMIT_UNEXPECTED_FILE
   错误说明: 意外的文件字段,请检查字段名是否正确
   相关字段: myfile
   提示: 请检查请求格式是否符合 multipart/form-data 规范

项目结构

node_test_server/
├── src/
│   ├── app.js                 # 主应用入口
│   ├── http/
│   │   ├── basic.js           # 基础测试接口
│   │   ├── upload.js          # 文件上传接口
│   │   └── download.js        # 文件下载接口
│   ├── websocket/
│   │   └── index.js           # WebSocket 服务
│   └── middleware/
│       └── requestLogger.js   # 详细请求日志中间件
├── files/                     # 上传文件存储目录
├── test-ws.html               # WebSocket 测试页面
├── package.json
├── docker-compose.yml
├── Dockerfile
└── README.md

常见问题排查

1. multipart/form-data 上传失败

检查项:

  • Content-Type 是否包含 boundary 参数
  • boundary 是否与请求体中的分隔符匹配
  • 文件字段名是否正确(/upload 接口要求字段名为 file
  • 请求体是否以正确的 boundary 开始和结束

正确格式示例:

Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW

------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain

(文件内容)
------WebKitFormBoundary7MA4YWxkTrZu0gW--

2. 流式上传失败

检查项:

  • 请求头是否包含 x-filename
  • Content-Type 是否正确设置
  • Content-Length 是否与实际数据大小匹配

3. WebSocket 连接失败

检查项:

  • 使用正确的 WebSocket 地址 ws://localhost:3000
  • 确保服务器已启动

技术栈

  • Node.js - 运行环境
  • Express - Web 框架
  • Multer - 文件上传中间件
  • ws - WebSocket 库

License

MIT

About

node test server supply mock interface for different kind http request and ws request

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors