All Projects → inlym → koa-to-serverless

inlym / koa-to-serverless

Licence: MIT license
只需要改一行代码就能够让你的 Koa 框架应用在 Serverless 架构环境上运行

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to koa-to-serverless

Koa Proxy
Proxy middleware for koa
Stars: ✭ 221 (+689.29%)
Mutual labels:  koa
koa-plus
The Koa framework extended for APIs. Optimized for security, scalability, and productivity.
Stars: ✭ 17 (-39.29%)
Mutual labels:  koa
chatroom
💬chat
Stars: ✭ 56 (+100%)
Mutual labels:  koa
Blog
若川的博客—学习源码整体架构系列8篇,前端面试高频源码,微信搜索「若川视野」关注我,长期交流学习~
Stars: ✭ 234 (+735.71%)
Mutual labels:  koa
ndaify-web
NDAify helps you keep your trade secrets under wraps 🔒
Stars: ✭ 33 (+17.86%)
Mutual labels:  koa
pupflare
A webpage proxy that request through Chromium (puppeteer) - can be used to bypass Cloudflare anti bot / anti ddos on any application (like curl)
Stars: ✭ 183 (+553.57%)
Mutual labels:  koa
Openapi Backend
Build, Validate, Route, Authenticate and Mock using OpenAPI
Stars: ✭ 216 (+671.43%)
Mutual labels:  koa
koa-shopify-auth
DEPRECATED Middleware to authenticate a Koa application with Shopify
Stars: ✭ 82 (+192.86%)
Mutual labels:  koa
teapot
Utilities for working with HTTP status codes, errors, and more
Stars: ✭ 14 (-50%)
Mutual labels:  koa
numvalidate
Phone number validation REST API
Stars: ✭ 54 (+92.86%)
Mutual labels:  koa
Strapi Sdk Javascript
🔌 Official JavaScript SDK for APIs built with Strapi.
Stars: ✭ 247 (+782.14%)
Mutual labels:  koa
Javascript Boilerplate
Node.js+Koa.js+PostgreSQL+React.js+Webpack+Mocha+Makefile, a starter kit for new apps
Stars: ✭ 253 (+803.57%)
Mutual labels:  koa
koa-ip-filter
koa middleware to filter request IPs or custom ID with glob patterns, array, string, regexp or matcher function. Support custom 403 Forbidden message and custom ID.
Stars: ✭ 23 (-17.86%)
Mutual labels:  koa
Koa Websocket
Light wrapper around Koa providing a websocket middleware handler that is koa-route compatible.
Stars: ✭ 224 (+700%)
Mutual labels:  koa
koa-subdomain
Simple and lightweight Koa middleware to handle multilevel and wildcard subdomains
Stars: ✭ 23 (-17.86%)
Mutual labels:  koa
Roa
async web framework inspired by koajs, lightweight but powerful.
Stars: ✭ 221 (+689.29%)
Mutual labels:  koa
bunjil
A GraphQL bastion server with schema merging, authentication and authorization with Policy Based Access Control
Stars: ✭ 25 (-10.71%)
Mutual labels:  koa
koa-whistle
This package is no longer maintained(该模块不再维护)
Stars: ✭ 11 (-60.71%)
Mutual labels:  koa
vue-koa2-login
🍥 Vue + Koa2 实现前后端注册登录流程
Stars: ✭ 23 (-17.86%)
Mutual labels:  koa
koa-mongoDB
😊😊Koa and mongoose build services
Stars: ✭ 24 (-14.29%)
Mutual labels:  koa

koa-to-serverless

只需要改一行代码就能够让你的 Koa 框架应用在 Serverless 架构环境上运行。

简介

Serverless 架构相对于自行部署的原生环境有巨大的优势(各云厂商对 Serverless 的介绍已经非常详细,这里就不赘述了),但是已有的代码想要直接迁移到 Serverless 架构上却不容易,主要面临以下几个困难:

(1)Serverless 架构环境与原生环境有所差异,原有的代码无法直接运行。

(2)各云厂商的 Serverless 实现都有所差异,无法兼容。

koa-to-serverless 就是为了解决这个问题诞生的。它能够让你只改一行代码就让你的原生 Koa 框架应用在 Serverless 架构环境上运行。

安装

直接使用 npm 命令安装即可:

npm i koa-to-serverless

使用

基础

下面这张图表示了使用原生环境和使用 Serverless 环境 Koa 框架代码的区别。虽然说改一行,实际上总共改2行,1行引入,1行使用。其他位置的代码你无需进行任何改动。

注意事项

(1)由于已经将请求的 body 植入了 app.request.body ,你无需再使用 koa-bodyparser 获取 body,请直接 去掉对应代码。

(2)其他 Koa 的中间件,目前未发现不能使用的。若你在使用中发现某个中间件使用异常,请向我提 issue。

扩展

除了完全兼容 Koa 框架外,koa-to-serverless 还向 Koa 框架的 ctx 参数植入了一些属性,你可以直接使用。例如:ctx.id 。

参数名 类型 说明
id string API网关带入的请求ID

配置

由于云厂商平台限制,部分参数需要在云厂商的 API 网关处进行配置才能获取到。请按照您使用的云厂商平台进行设置。

阿里云

以下是 API 网关配置的注意事项:

(1)【请求路径】:配置为【 / 】,并勾选【匹配所有子路径】。路径这块执行逻辑API网关层透传,由 Koa 框架的 koa-router 中间件接管处理。

(2)【HTTP Method】:配置为【ANY】,相当于请求方法也是透传,由 Koa 框架的 koa-router 中间件接管处理。

(3)【入参请求模式】:配置为【入参透传】。

API网关配置1.png

以下几个参数需要 API 网关提供,请直接按照图示参数名称填写。

API网关配置2.png

其他云平台

开发中,待支持 ...

进阶

原理剖析

koa-to-serverless 实际上使用以下流程给 Koa 框架提供了一个兼容层。

流程.png

以下这张图可以大致抽象地描述了 koa-to-serverless 作为兼容层提供的能力:

框架扩展

基于以上流程,以后的扩展会非常容易,扩展主要包含2个方面:

(1)支持 Koa 框架在其他云厂商的 Serverless 架构环境上运行。

(2)支持其他框架,例如 Express 等,在 Serverless 架构环境上运行。

以上2点扩展实际上都非常容易:

(1)第1个扩展只需要增加步骤2对应的触发器处理函数就可以了。

(2)第2个扩展只需要识别框架类型,然后改动步骤4,调用对应的框架的入口函数就可以了。

以上扩展以后会逐渐支持。

以下是支持情况:

Koa
阿里云 API 网关触发器
阿里云 HTTP 触发器
腾讯云 API 网关触发器
华为云 API 网关触发器
百度智能云 API 网关触发器
AWS API 网关触发器

反馈

任何使用上的问题,以及意见和建议,都可以向我提 issue,地址: https://github.com/inlym/koa-to-serverless/issues

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].