All Projects → ryouaki → koa

ryouaki / koa

Licence: MIT license
A golang framework like koa.js and has the best performance with net/http.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to koa

Koa Template
Starter template for Nuxt.js with KoaJS.
Stars: ✭ 374 (+1146.67%)
Mutual labels:  koa, koajs
Koa Typeorm Starter
Starter project for using koa with TS and TypeORM
Stars: ✭ 23 (-23.33%)
Mutual labels:  koa, koajs
Koa2 Note
《Koa2进阶学习笔记》已完结🎄🎄🎄
Stars: ✭ 4,725 (+15650%)
Mutual labels:  koa, koajs
restria
Entria's REST API boilerplate
Stars: ✭ 25 (-16.67%)
Mutual labels:  koa, koajs
Cottage
Simple, fast HTTP router on koa.js.
Stars: ✭ 103 (+243.33%)
Mutual labels:  koa, web-framework
Koahub
KoaHub.js -- 中文最佳实践Node.js Web快速开发框架。支持Koa.js, Express.js中间件。当前项目已停止维护,推荐使用Doodoo.js
Stars: ✭ 308 (+926.67%)
Mutual labels:  koa, koajs
Vue Chat
👥Vue全家桶+Socket.io+Express/Koa2打造一个智能聊天室。
Stars: ✭ 887 (+2856.67%)
Mutual labels:  koa, koajs
polix
🚀 Node.js Web Framework
Stars: ✭ 32 (+6.67%)
Mutual labels:  koa, koajs
Koach Javascript
Production ready Koa2 boilerplate.
Stars: ✭ 79 (+163.33%)
Mutual labels:  koa, koajs
Doodoo.js
Doodoo.js -- 中文最佳实践Node.js Web快速开发框架,支持Koa.js, Express.js中间件。
Stars: ✭ 57 (+90%)
Mutual labels:  koa, koajs
koa-server
🗄️ GraphQL Back-end Server with Relay, Koa, MongoDB and Mongoose
Stars: ✭ 31 (+3.33%)
Mutual labels:  koa, koajs
koa-subdomain
Simple and lightweight Koa middleware to handle multilevel and wildcard subdomains
Stars: ✭ 23 (-23.33%)
Mutual labels:  koa, koajs
Koajs Design Note
《Koa.js 设计模式-学习笔记》已完结 😆
Stars: ✭ 520 (+1633.33%)
Mutual labels:  koa, koajs
Koahub Demo
koahub+async/await+mysql
Stars: ✭ 15 (-50%)
Mutual labels:  koa, koajs
Cool Admin Api
cool-admin-api 是基于egg.js、typeorm、jwt等封装的api开发脚手架、快速开发api接口
Stars: ✭ 188 (+526.67%)
Mutual labels:  koa, koajs
koahub-cli
KoaHub CLI -- KoaHub.js的开发工具,自动babel编译 ES6/7(Generator Function, Class, Async & Await)并且文件修改后自动重启。
Stars: ✭ 16 (-46.67%)
Mutual labels:  koa, koajs
bay
The framework
Stars: ✭ 20 (-33.33%)
Mutual labels:  web-framework
graphql-compose-elasticsearch
Graphql App using Node with typescript, KOA framework and Elasticsearch
Stars: ✭ 40 (+33.33%)
Mutual labels:  koa
koa-better-error-handler
A better error-handler for Lad and Koa. Makes `ctx.throw` awesome (best used with koa-404-handler)
Stars: ✭ 51 (+70%)
Mutual labels:  koa
midwayjs-crud
基于 Typescript+MidwayJs+Nacos 的微服务开发架构
Stars: ✭ 45 (+50%)
Mutual labels:  koa

koa.go

Expressive HTTP middleware framework for Golang to make web applications and APIs more enjoyable to write like Koa.js. Koa's middleware stack flows in a stack-like manner, allowing you to perform actions downstream then filter and manipulate the response upstream.

Koa is not bundled with any middleware.

Benchmarks

ryouaki/koa has the best performance:

name qps transactions/s
koa(golang) 12598080 42014.05
gin 11936723 39812.96
echo 12347680 41179.66
beego 11051408 36856.09
koa(nodejs) 8042021 26818.72

Installation

  $ go get github.com/ryouaki/koa

Hello Koa

  package main

  import (
    "fmt"

    "github.com/ryouaki/koa"
  )

  func main() {
    app := koa.New() // 初始化服务对象

    // 设置api路由,其中var为url传参
    app.Get("/", func(ctx *koa.Context, next koa.Next) {
      ctx.SetBody([]byte("Hello Koa"))
    })

    err := app.Run(8080) // 启动
    if err != nil {      // 是否发生错误
      fmt.Println(err)
    }
  }

Middleware

Koa is a middleware framework, Here is an example of logger middleware with each of the different functions:

  package plugin

  import (
    "fmt"
    "time"

    "github.com/ryouaki/koa"
  )

  // Log out about the duration for request.
  func Duration(ctx *koa.Context, next koa.Next) {
    startTime := time.Now() // 开始计时
    next(nil) // 执行后续操作
    d := time.Now().Sub(startTime) // 计算耗时
    // 打印结果
    fmt.Println(time.Date(startTime.Year(),
      startTime.Month(),
      startTime.Day(),
      startTime.Hour(),
      startTime.Minute(),
      startTime.Second(), 0, time.Local),
      ctx.URL,
      "Request cost: ",
      float64(d)/float64(time.Millisecond), "ms")
  }

Koa Application

  type Application struct {
    handles []Handle
    _cb     Handler
  }

  type Handle struct {
    path     string
    method  string
    handler Handler
  }

  // Handler Func
  type Handler func(ctx *Context, n Next)

  // Next Func
  type Next func()

  // New a Koa instance
  func New() *Application
  // Add a middleware for koa application
  /**
  * params: path<string|option> path for request
  * params: callback<koa.Handler|option> cb for request
  * params: callback ...
  */
  func (app *Application) Use(argus ...interface{})
  // Get func
  func (app *Application) Get(path string, cbFunc ...Handler) error
  // Post func
  func (app *Application) Post(path string, cbFunc ...Handler) error
  // Delete func
  func (app *Application) Delete(path string, cbFunc ...Handler) error
  // Patch func
  func (app *Application) Patch(path string, cbFunc ...Handler) error 
  // Put func
  func (app *Application) Put(path string, cbFunc ...Handler) error
  // Options func
  func (app *Application) Options(path string, cbFunc ...Handler) error
  // Head func
  func (app *Application) Head(path string, cbFunc ...Handler) error
  // Run func
  func (app *Application) Run(port int) error 
  // RunTLS func
  func (app *Application) RunTLS(port int, certFile string, keyFile string) error 

About Context

  type Context struct {
    Res      http.ResponseWriter    // Res the response object
    Req      *http.Request          // Req the request object from client
    Url      string                 // Url
    Path     string                 // Rqeust path
    Method   string                 // Method like Get, Post and others
    Status   int                    // Status you want to let client konw for the request
    MatchURL string                 // For the router path
    Body     []uint8                // The body from client
    Query    map[string]([]string)  // The Query from request's path
    Params   map[string](string)    // The Params from request's path
  }

  // New a context for request
  func NewContext(res http.ResponseWriter, req *http.Request) *Context
  // Get Header from request func
  func (ctx *Context) GetHeader(key string) []string 
  // Set header item for Response func
  func (ctx *Context) SetHeader(key string, value string)
  // Get cookie from request func
  func (ctx *Context) GetCookie(key string) *http.Cookie
  // Set cookie for response func
  func (ctx *Context) SetCookie(cookie *http.Cookie)
  // Set data for context func
  func (ctx *Context) GetData(key string) interface{}
  // Get data from contexxt  func
  func (ctx *Context) SetData(key string, value interface{})
  // Set data for response
  func (ctx *Context) SetBody(data []byte)

Components

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].