Skip to content

🚀 NestJS + Auth + BaseController + Upload + ExceptionFilter + Frontend Demo 🚀

License

Notifications You must be signed in to change notification settings

BoBoooooo/BoBo-NestJS-Server

Repository files navigation

BoBo-NestJS-Server

Nest Logo

A progressive Node.js framework for building efficient and scalable server-side applications, heavily inspired by Angular.

NPM Version Package License NPM Downloads Travis Linux Coverage Gitter Backers on Open Collective Sponsors on Open Collective

🚀 轻量级 NestJS-API 脚手架,欢迎 Star!

Demo

域名&服务器已过期...暂时没法访问,抱歉

1. 开始使用

2.目录结构

├── public                     # public
│   └── index.html             # 测试socket.io页面
├── src                        # src
│   ├── config                 # 环境变量等配置文件
│   ├── core                   # 通用定义文件
│   ├── entities               # 数据库ORM Entity
│   ├── events                 # webSocket
│   ├── filter                 # 全局异常filter
│   ├── guards                 # 路由守卫(鉴权)
│   ├── interceptor            # interceptor
│   ├── logs                   # logs
│   ├── middleware             # middleware logs
│   ├── module                 # 各个业务模块,包含base module
│   ├── utils                  # 工具函数
├── test                       # 测试

3. 功能

  • 登录、JWT鉴权

  • Socket.IO

    默认Socket.IO监听8080端口 /src/events/events.gateway.ts

    client端实现查看 /public/index.html

  • BaseController BaseService 封装基础CRUD接口

    • /xxx/add
    • /xxx/update
    • /xxx/delete
    • /xxx/list
    • /xxx/tree
    • /xxx/detail
  • list接口支持高级查询,具体实现查看BaseService

示例:
curl - POST /users/list
  {
    "searchCondition":[{
      "field":"userName",
      "operator":"like",
      "value":"ce"
    },
    {
      "field":"userName",
      "operator":"like",
      "value":"shi"
    }],
    "pageIndex":1,
    "pageSize":1,
    "orderCondition":"id desc"
  }
===
最后生成的sql:
select * from users where userName like '%ce%' and userName like '%shi%' order by id desc limit 1,1
  • 全局http异常处理以及代码异常处理

  • log 日志

  • HTTP返回值统一ResultBean封装

  • 文件上传,静态资源服务

  • 一键生成数据库模型 yarn db (typeorm-model-generator)

4. 部署

// 项目提供了 /src/config/env/dev,prod,test 三种环境变量配置文件

// main入口监听端口号
await app.listen(process.env.PORT || 3000);

// 采用不同的npm命令实现多环境部署
cross-env NODE_ENV=production   // npm 命令 cross-env指定当前环境
PORT=3000 // 指定监听端口

具体流程

  • npm run build
  • 拷贝dist文件夹
  • 拷贝package.json
  • 服务器上执行 npm install 安装所需依赖 (此处大坑,依赖并不会被打包到dist文件夹中)
  • npm run start:prod / npm run pm2

About

🚀 NestJS + Auth + BaseController + Upload + ExceptionFilter + Frontend Demo 🚀

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages