GithubHelp home page GithubHelp logo

koa-restful-quick-start's Introduction

koa-restful-quick-start

koa-restful 快速启动工程



查看Demo/在线测试

serve模式运行截图

快速开始

# 克隆项目
git clone https://github.com/WangMingHua111/koa-restful-quick-start.git
# 进行工作目录
cd koa-restful-quick-start
# 安装依赖
npm install
# dev模式启动
npm run dev

本地开发

npm run dev

构建生产环境

npm run build

运行服务

npm run serve

Http Serve

http://localhost:3000

运行截图

dev 模式

dev模式运行截图

线上模式

serve模式运行截图

自动生成 api swagger 文档

swagger文档

QQ 交流群

QQ交流群

Demo 控制器

import { Authorize, CacheService, Controller, FromBody, FromHeader, FromQuery, FromRoute, HttpDelete, HttpGet, HttpHead, HttpOptions, HttpPatch, HttpPost, HttpPut, Injection, LoggerService } from '@wangminghua/koa-restful'
import { CookieAuthorization, JwtBearerAuthorization, SimpleAuthorize } from '@wangminghua/koa-restful/extra'
import { Context } from 'koa'

/**
 * Demo服务
 */
@Controller()
export class DemoController {
  /**
   * 日志
   */
  @Injection()
  logger: LoggerService
  /**
   * 缓存
   */
  @Injection()
  cache: CacheService

  @Injection()
  bearer: JwtBearerAuthorization

  @Injection()
  cookie?: CookieAuthorization

  /**
   * 执行登录
   * @param ctx
   * @returns
   */
  @HttpGet()
  login(ctx: Context) {
    const token = this.bearer.sign({
      data: new Date().toLocaleString(),
    })
    // 如果有 cookie 鉴权,写入cookie
    if (this.cookie) {
      const cookieToen = this.cookie.sign({
        data: new Date().toLocaleString(),
      })
      ctx.cookies.set(this.cookie.authorityHeader, cookieToen)
    }
    return token
  }

  /**
   * 测试Authorize
   * @returns
   */
  @Authorize()
  @HttpGet()
  auth() {
    return new Date().toLocaleString()
  }

  /**
   * 测试SimpleAuthorize
   * @returns
   */
  @SimpleAuthorize(['Bearer', 'Cookie'])
  @HttpGet()
  auth2() {
    return new Date().toLocaleString()
  }

  /**
   * 测试接口,带查询参数
   * @returns
   */
  @HttpGet()
  @HttpPost()
  @HttpPut()
  @HttpDelete()
  @HttpHead()
  @HttpPatch()
  @HttpOptions()
  async test(@FromQuery() name: string = 'hello'): Promise<TestModel> {
    const localeTime = await this.cache.get('localeTime', async () => {
      const locale = new Date().toLocaleString()
      return locale
    })

    this.logger?.info(`localeTime >>> ${localeTime}`)

    return {
      name: name,
      value: 99,
    }
  }
  /**
   * 提交body
   * @param body
   * @returns
   */
  @HttpPost()
  test1(@FromBody() body: TestModel) {
    return {
      ...body,
      name: 'hello ' + body.name,
    }
  }

  /**
   * 路由参数和请求头参数
   * @param body
   * @returns
   */
  @HttpGet('test2/:id')
  test2(@FromRoute() id: string, @FromHeader() name: string = 'hipy') {
    return {
      id,
      name,
    }
  }
  /**
   * 清理缓存
   * @param cacheKey 缓存Key
   */
  @HttpPost()
  cleanCache(@FromQuery() cacheKey = 'localeTime') {
    this.cache.delete(cacheKey)
  }
}

/**
 * 测试模型
 */
interface TestModel {
  /**
   * 名称
   */
  name: string
  /**
   * 数据值
   */
  value: number
}

koa-restful-quick-start's People

Contributors

wangminghua111 avatar

Stargazers

 avatar

Watchers

 avatar

koa-restful-quick-start's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.