npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

buse_imgserver

v1.0.0

Published

基于 Node.js + Express + TypeScript 的图片上传服务器 - 可复用的 npm 包

Readme

图片上传服务器

基于 Node.js + Express + TypeScript 的图片上传服务器,支持单个和批量图片上传。

功能特性

  • ✅ 单个图片上传
  • ✅ 批量图片上传
  • ✅ 文件类型验证(支持 jpg, jpeg, png, gif, webp)
  • ✅ 文件大小限制(默认 10MB)
  • ✅ 自动生成唯一文件名
  • ✅ 静态文件访问
  • ✅ 完善的错误处理
  • ✅ TypeScript 类型支持

快速开始

1. 安装依赖

npm install

2. 配置环境变量

复制 .env.example.env 并根据需要修改配置:

cp .env.example .env

3. 启动开发服务器

npm run dev

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

4. 生产环境部署

# 编译 TypeScript
npm run build

# 启动生产服务器
npm start

# 或者一键编译并启动
npm run start:prod

API 接口

1. 上传单个图片

接口地址: POST /api/upload

请求参数:

  • image: 图片文件(form-data)

请求示例(curl):

curl -X POST http://localhost:3000/api/upload \
  -F "image=@/path/to/your/image.jpg"

成功响应:

{
  "success": true,
  "message": "图片上传成功",
  "data": {
    "originalName": "image.jpg",
    "filename": "1234567890-uuid.jpg",
    "size": 102400,
    "sizeFormatted": "100.00KB",
    "mimetype": "image/jpeg",
    "path": "/uploads/1234567890-uuid.jpg",
    "fullUrl": "http://localhost:3000/uploads/1234567890-uuid.jpg",
    "uploadTime": "2024-01-01T00:00:00.000Z"
  }
}

2. 批量上传图片

接口地址: POST /api/upload/multiple

请求参数:

  • images: 图片文件数组(form-data,最多10个)

请求示例(curl):

curl -X POST http://localhost:3000/api/upload/multiple \
  -F "images=@/path/to/image1.jpg" \
  -F "images=@/path/to/image2.png"

成功响应:

{
  "success": true,
  "message": "成功上传 2 个图片",
  "data": {
    "count": 2,
    "files": [
      {
        "originalName": "image1.jpg",
        "filename": "1234567890-uuid1.jpg",
        "size": 102400,
        "sizeFormatted": "100.00KB",
        "mimetype": "image/jpeg",
        "path": "/uploads/1234567890-uuid1.jpg",
        "fullUrl": "http://localhost:3000/uploads/1234567890-uuid1.jpg"
      },
      {
        "originalName": "image2.png",
        "filename": "1234567891-uuid2.png",
        "size": 204800,
        "sizeFormatted": "200.00KB",
        "mimetype": "image/png",
        "path": "/uploads/1234567891-uuid2.png",
        "fullUrl": "http://localhost:3000/uploads/1234567891-uuid2.png"
      }
    ],
    "uploadTime": "2024-01-01T00:00:00.000Z"
  }
}

3. 健康检查

接口地址: GET /health

响应:

{
  "status": "ok",
  "timestamp": "2024-01-01T00:00:00.000Z"
}

4. 访问上传的图片

接口地址: GET /uploads/{filename}

示例:

http://localhost:3000/uploads/1234567890-uuid.jpg

错误响应

当上传失败时,会返回错误信息:

{
  "success": false,
  "message": "错误描述",
  "error": "详细错误信息"
}

常见错误:

  • 文件大小超出限制
  • 不支持的文件类型
  • 未选择文件
  • 服务器内部错误

项目结构

imgServer/
├── src/
│   ├── config/          # 配置文件
│   │   └── index.ts
│   ├── controllers/     # 控制器
│   │   └── uploadController.ts
│   ├── middleware/      # 中间件
│   │   ├── errorHandler.ts
│   │   └── upload.ts
│   ├── routes/          # 路由
│   │   └── upload.ts
│   ├── utils/           # 工具函数
│   │   └── fileHelper.ts
│   ├── app.ts           # Express 应用配置
│   └── server.ts        # 服务器入口
├── uploads/             # 上传文件存储目录
├── .env                 # 环境变量配置
├── .env.example         # 环境变量示例
├── .gitignore
├── nodemon.json         # Nodemon 配置
├── package.json
├── tsconfig.json        # TypeScript 配置
└── README.md

配置说明

.env 文件中可以配置以下参数:

| 参数 | 说明 | 默认值 | |------|------|--------| | PORT | 服务器端口 | 3000 | | UPLOAD_DIR | 上传文件保存目录 | uploads | | MAX_FILE_SIZE | 最大文件大小(字节) | 10485760 (10MB) |

技术栈

  • Node.js - JavaScript 运行环境
  • Express - Web 框架
  • TypeScript - 类型安全的 JavaScript
  • Multer - 文件上传中间件
  • dotenv - 环境变量管理
  • uuid - 生成唯一标识符
  • cors - 跨域资源共享

开发命令

# 开发模式(热重载)
npm run dev

# 编译 TypeScript
npm run build

# 启动生产服务器
npm start

# 编译并启动
npm run start:prod

注意事项

  1. 确保 uploads 目录有写入权限
  2. 生产环境建议使用 Nginx 等反向代理服务器
  3. 建议配置文件大小限制以防止恶意上传
  4. 可以根据需要调整支持的图片格式
  5. 生产环境建议使用专业的对象存储服务(如 AWS S3、阿里云 OSS 等)

License

ISC