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

@jinyang756/api-server

v1.1.5

Published

API服务部署与多场景扩展项目 - 专为1核1GB内存服务器优化的完整API解决方案

Readme

Linux-CentOS-API 服务器部署项目

项目简介

这是一个为1核1GB内存服务器优化的完整API解决方案,集成了多种实用功能和服务,包括用户管理、文章管理、菜谱分享、工具集合、文件上传、邮箱验证、地图服务、实时通讯、打印服务、新闻资讯以及RustDesk远程桌面集成。

功能特性

核心功能

  • 🔐 用户认证系统(注册/登录/JWT)
  • 📝 文章管理系统
  • 🍳 菜谱分享平台
  • 🛠️ 实用工具集合
  • 📁 文件上传服务(图片/文档/音频)
  • 📧 邮箱验证服务
  • 🗺️ Google地图API集成
  • 💬 实时通讯系统(Socket.IO)
  • 🖨️ 打印机API服务
  • 📰 无国界医生新闻API
  • 🖥️ RustDesk远程桌面集成

安全特性

  • 🔒 JWT Token认证
  • 🛡️ Helmet安全头部
  • ⚡ 速率限制防护
  • 📦 输入验证(Joi)
  • 🔐 环境变量配置
  • 🌐 CORS安全策略

目录

一、环境准备(服务器端)

1. 系统要求

  • CentOS 8.2
  • Node.js >= 18.x (推荐18 LTS)
  • MongoDB >= 4.x
  • PM2 (进程管理)
  • Nginx (反向代理)

2. 安装依赖

# 更新系统
sudo yum update -y

# 安装Node.js 18 LTS (推荐)
curl -fsSL https://rpm.nodesource.com/setup_18.x | sudo bash -
sudo yum install -y nodejs

# 或安装Node.js 20 (最新稳定版)
# curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -
# sudo yum install -y nodejs

# 验证安装
node --version  # 应该显示 v18.x.x 或 v20.x.x
npm --version

3. 安装MongoDB

# 添加MongoDB仓库
sudo tee /etc/yum.repos.d/mongodb-org-4.4.repo <<EOF
[mongodb-org-4.4]
name=MongoDB Repository
baseurl=https://repo.mongodb.org/yum/redhat/8/mongodb-org/4.4/x86_64/
gpgcheck=1
enabled=1
gpgkey=https://www.mongodb.org/static/pgp/server-4.4.asc
EOF

# 安装MongoDB
sudo yum install -y mongodb-org

# 启动MongoDB
sudo systemctl start mongod
sudo systemctl enable mongod

# 验证MongoDB
mongo --eval 'db.runCommand({ connectionStatus: 1 })'

二、项目部署

方法一:全自动一键部署(推荐用于从0开始部署)

# 下载一键部署脚本
wget https://raw.githubusercontent.com/jinyang756/api-server/master/full-deploy.sh

# 给脚本添加执行权限
chmod +x full-deploy.sh

# 以root权限运行部署脚本
sudo ./full-deploy.sh

方法二:传统部署(推荐用于1G1H服务器)

1. 克隆项目

git clone https://github.com/jinyang756/api-server.git
cd api-server

2. 安装依赖

npm install

3. 配置环境变量

# 复制环境变量模板
cp .env.example .env

# 编辑环境变量
nano .env

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

# 应用配置
NODE_ENV=production
PORT=3003

# JWT密钥(请更改为强随机字符串)
JWT_SECRET=your-super-secret-jwt-key-change-in-production

# MongoDB连接字符串
MONGODB_URI=mongodb://localhost:27017/api_server

# 邮件服务配置(根据实际情况修改)
EMAIL_HOST=smtp.example.com
EMAIL_PORT=587
[email protected]
EMAIL_PASS=your-email-password

# Google Maps API密钥(如需要)
GOOGLE_MAPS_API_KEY=your-google-maps-api-key

4. 启动服务

# 使用PM2启动服务
npx pm2 start app.js --name api-server

# 设置开机自启
npx pm2 startup
npx pm2 save

5. 配置Nginx反向代理

# 创建Nginx配置文件
sudo nano /etc/nginx/conf.d/api.conf

添加以下配置:

server {
    listen 80;
    server_name your-domain.com;  # 替换为你的域名

    location / {
        proxy_pass http://localhost:3003;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_cache_bypass $http_upgrade;
    }
}

重启Nginx:

sudo systemctl restart nginx

方法三:容器化部署(Docker)

1. 安装Docker和Docker Compose

# 安装Docker
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum install -y docker-ce docker-ce-cli containerd.io

# 启动Docker
sudo systemctl start docker
sudo systemctl enable docker

# 安装Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/download/v2.20.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
sudo chmod +x /usr/local/bin/docker-compose

2. 使用一键部署脚本

# 给脚本添加执行权限
chmod +x quick-deploy.sh

# 运行部署脚本
./quick-deploy.sh

对于Windows系统,使用PowerShell脚本:

# 设置执行策略
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

# 运行部署脚本
.\quick-deploy.ps1

三、API接口文档

认证相关

  • POST /api/users/register - 用户注册
  • POST /api/users/login - 用户登录
  • GET /api/users/profile - 获取用户信息(需认证)

内容管理

  • GET /api/articles - 获取文章列表

  • POST /api/articles - 创建文章(需认证)

  • GET /api/articles/:id - 获取文章详情

  • PUT /api/articles/:id - 更新文章(需认证)

  • DELETE /api/articles/:id - 删除文章(需认证)

  • GET /api/recipes - 获取菜谱列表

  • POST /api/recipes - 创建菜谱(需认证)

  • GET /api/recipes/:id - 获取菜谱详情

  • PUT /api/recipes/:id - 更新菜谱(需认证)

  • DELETE /api/recipes/:id - 删除菜谱(需认证)

工具接口

  • GET /api/tools/qrcode - 生成二维码
  • POST /api/tools/pdf - 生成PDF文档
  • POST /api/tools/image-resize - 图片缩放

文件上传

  • POST /api/upload - 上传文件
  • GET /uploads/:filename - 访问上传的文件

邮箱服务

  • POST /api/email/send - 发送邮件

地图服务

  • GET /api/map/geocode - 地址编码
  • GET /api/map/reverse-geocode - 反向地址编码

通讯服务

  • POST /api/communication/send - 发送消息
  • GET /api/communication/history/:userId - 获取消息历史

打印服务

  • POST /api/printer/print - 打印文档

新闻资讯

  • GET /api/news/msf - 获取无国界医生新闻

RustDesk远程控制

  • POST /api/rustdesk/session/start - 发起远程控制会话
  • POST /api/rustdesk/call/start - 发起语音通话
  • POST /api/rustdesk/session/end - 结束会话或通话
  • GET /api/rustdesk/session/status - 获取会话状态

四、安全配置

环境变量配置

项目使用环境变量来管理敏感配置。请确保在生产环境中正确配置以下变量:

# 复制示例配置文件
cp .env.example .env

# 编辑配置文件
nano .env

关键安全配置项:

  • JWT_SECRET: JWT签名密钥,应使用强随机字符串
  • MONGODB_URI: MongoDB连接字符串,包含认证信息
  • EMAIL_USEREMAIL_PASS: 邮件服务认证信息

安全特性说明

  1. JWT认证:所有需要认证的接口都使用JWT Token进行身份验证
  2. Helmet安全头:自动添加安全相关的HTTP头
  3. 速率限制:防止API滥用,默认每15分钟100次请求
  4. 输入验证:使用Joi库对所有用户输入进行严格验证
  5. CORS策略:配置合理的跨域资源共享策略
  6. 文件上传限制:限制上传文件大小和类型

生产环境安全建议

  1. 使用HTTPS:

    # 在Nginx配置中添加SSL证书
    ssl_certificate /path/to/your/certificate.crt;
    ssl_certificate_key /path/to/your/private.key;
  2. 定期更新依赖:

    npm audit
    npm audit fix
  3. 监控日志:

    # 查看PM2日志
    npx pm2 logs
       
    # 查看Nginx访问日志
    tail -f /var/log/nginx/access.log
       
    # 查看Nginx错误日志
    tail -f /var/log/nginx/error.log

五、使用npm包

现在也可以通过npm直接安装使用:

# 安装
npm install @jinyang756/api-server

# 启动服务
npx pm2 start node_modules/@jinyang756/api-server/app.js

# 使用远程控制API
npx api-server remote-control --server http://your-server.com status

六、故障排除

常见问题

  1. 端口被占用

    # 查看端口占用情况
    netstat -tlnp | grep :3003
       
    # 杀死占用进程
    kill -9 <PID>
  2. MongoDB连接失败

    # 检查MongoDB状态
    sudo systemctl status mongod
       
    # 启动MongoDB
    sudo systemctl start mongod
  3. PM2服务异常

    # 查看服务状态
    npx pm2 list
       
    # 重启服务
    npx pm2 restart api-server
       
    # 查看日志
    npx pm2 logs api-server

日志查看

# 查看应用日志
npx pm2 logs

# 查看Nginx日志
tail -f /var/log/nginx/access.log
tail -f /var/log/nginx/error.log

七、性能优化建议

  1. 数据库索引优化

    // 在MongoDB中为常用查询字段创建索引
    db.articles.createIndex({ "createdAt": -1 })
    db.users.createIndex({ "email": 1 }, { unique: true })
  2. 缓存策略

    # 安装Redis
    sudo yum install redis
    sudo systemctl start redis
    sudo systemctl enable redis
  3. 负载均衡(适用于多实例部署)

    upstream api_backend {
        server 127.0.0.1:3003;
        server 127.0.0.1:3004;
    }
       
    server {
        listen 80;
        location / {
            proxy_pass http://api_backend;
        }
    }

八、更新和维护

更新项目

# 拉取最新代码
git pull origin master

# 更新依赖
npm install

# 重启服务
npx pm2 restart api-server

备份数据

# 备份MongoDB数据
mongodump --db api_server --out /backup/

# 恢复MongoDB数据
mongorestore --db api_server /backup/api_server/

九、文档资源

为了更好地使用本项目,我们提供了丰富的文档资源:

开发文档

部署文档

集成文档

使用手册

  • USER_GUIDE.md - 详细的用户使用手册,包含所有API接口的使用方法

十、许可证

本项目采用MIT许可证,详见LICENSE文件。

十一、联系方式

如有任何问题或建议,请通过以下方式联系: