{"id":20859660,"url":"https://github.com/inlym/foi","last_synced_at":"2026-04-20T23:31:35.755Z","repository":{"id":109750762,"uuid":"257659429","full_name":"inlym/foi","owner":"inlym","description":"基于 Serverless 架构的 Node.js Web 开发框架，提供和 Koa 一致的 API，可跨云平台使用。","archived":false,"fork":false,"pushed_at":"2020-08-16T03:53:23.000Z","size":74,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-12-28T13:31:34.220Z","etag":null,"topics":["faas","framework","javascript","nodejs","serverless","serverless-framework","toolkit"],"latest_commit_sha":null,"homepage":"https://foi.inlym.com/","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/inlym.png","metadata":{"files":{"readme":"README.md","changelog":"HISTORY.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-04-21T17:03:31.000Z","updated_at":"2020-09-16T08:43:27.000Z","dependencies_parsed_at":"2023-03-22T00:04:05.436Z","dependency_job_id":null,"html_url":"https://github.com/inlym/foi","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/inlym/foi","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inlym%2Ffoi","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inlym%2Ffoi/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inlym%2Ffoi/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inlym%2Ffoi/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/inlym","download_url":"https://codeload.github.com/inlym/foi/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/inlym%2Ffoi/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32070586,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-20T21:26:33.338Z","status":"ssl_error","status_checked_at":"2026-04-20T21:26:22.081Z","response_time":94,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["faas","framework","javascript","nodejs","serverless","serverless-framework","toolkit"],"created_at":"2024-11-18T04:51:00.041Z","updated_at":"2026-04-20T23:31:35.721Z","avatar_url":"https://github.com/inlym.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# foi\n基于 Serverless 架构的 Node.js Web 开发框架，提供和 Koa 一致的 API，可跨云平台使用。\n\n\n\u003e GitHub：[https://github.com/inlym/foi](https://github.com/inlym/foi)\n\n\u003e 欢迎 star\n\n\n\n## 简介\nfoi 是一款基于 Serverless 架构的 Node.js Web 开发框架，提供和知名框架 Koa 一致的 API，可跨云平台使用。严格意义上来说，foi 并不是一个完整的框架，只是提供了一个方便开发 Serverless 应用的中间件。foi 主要做了以下两项工作：\n\n1. 统一对接各大云平台的 Serverless 实现\n1. 将封装好的 API 以 Koa 框架风格的 API 输出供开发者使用\n\n\n\n用以下的架构图表示 foi 的功能：\n![](https://img.inlym.com/6787f581fa804c24889ae31876ddd59f.jpg)\n\n\n\n\n## 优点\n### 跨多云平台\n各云平台（阿里云、腾讯云、华为云、AWS等）对于 Serverless 的实现存在一定的差异，使用上也存在很大的差异，foi 针对各云平台的 API  做了兼容，使开发者在使用时无需阅读各云平台的开发文档，直接以 Koa 的 API 开发即可。foi 使得一份代码能够在所有云平台的 Serverless 架构服务上部署和运行。\n\n\n### 低迁移成本\n从 Koa 框架迁移至 foi 框架，只需要更改 2 行代码即可使用。这里面 foi 框架做了大量的底层工作，兼容 Koa 的 API。（详情点击查看 从 Koa 迁移 foi ）\n\n\n另外，绝大多数的 Koa 中间件都可以在 foi 中使用。（由于 Serverless 架构限制，部分中间件无法使用。）\n\n\n### 低学习成本\n由于对开发者提供了与 Koa 一致的 API，因此开发无需额外花费时间学习 foi 本身，只需要根据文档说明对 foi 进行安装和使用即可，与 Koa 的差异仅在于初始化的 2 行代码。\n\n\n\n\n\n\n## 安装\nfoi 的安装非常简单，你可以直接使用 npm 进行安装：\n```bash\nnpm install foi\n```\n\n\n如果访问速度较慢，建议使用 cnpm 安装：（如需查看 cnpm 的安装方式，请 点击 ）\n```bash\ncnpm install foi\n```\n\n\n\n\n## 使用\n### hello world\n以下是一个输出响应（response）的主体为 'hello world' 的典型使用方式：\n```javascript\n'use strict'\nconst Foi = require('foi')\n\n// Serverless 架构结构体\nmodule.exports.handler = function (event, context, callback) {\n\t// 初始化 app\n\tconst app = new Foi({ event, context, callback })\n\n\t// 主体部分使用 app.use, 和 Koa 一致的 API\n\tapp.use(ctx =\u003e {\n\t\tctx.body = 'hello world'\n\t})\n\n\t// 假的 listen, 兼容 Koa, 建议使用 app.init()\n\tapp.listen()\n}\n```\n\n\n你可以看出，在使用上，和 Koa 几乎没有任何区分，Koa 中在 app.use( ) 中使用的代码，均能在 foi 中运行。\n\n### 初始化选项\n\n初始化的标准格式是：\n```javascript\nconst app = new Foi(options)    // options 是一个对象\n```\n\n\n针对不同的云平台和触发器，对应的 options 参数略有不同，其中包含了一些公共参数，和一些触发器的私有参数。\n\n\n#### 公共参数\n```javascript\nconst options = {\n\tslient: false,    // 是否禁止打印 foi 框架日志，默认 false，可选\n\ttrigger: 'aliyun-apigw',    // 触发器，阿里云API网关触发器为'aliyun-apigw'\n}\n```\n\n\n#### 阿里云 - API网关\n```javascript\nconst options = {\n\tevnet: event,\n\tcontext: context,\n\tcallback: callback,    // Serverless 传入的三要素，注意这里的属性值要和定义的变量名一致\n}\n```\n\n\n\n## 配置\n\n由于框架限制，部分参数需要在云平台进行配置才可以获取到。请按照你使用的触发器进行配置。\n\n\n#### 阿里云 - API网关\n（1）路径参数 - params\n通过以下方式配置后，你可以使用 ctx.params 获取路径参数对象\n![](https://img.inlym.com/7e8dc3657385440599f2cd384d232d29.jpg)\n\n（2）其他参数 - ip, host, protocol\n要获取 ip, host, protocol 参数，请在API网关处进行如下配置，否则会获取不到对应参数。\n![](https://img.inlym.com/560a1831f2274b019ae96a3022f9ad36.jpg)\n\n\n\n\n## 从 Koa 迁移 foi\n### 迁移方式\n从以上的使用方式看出，最核心的 app.use( ) 不需要改动。我们先来看一个最普通的 Koa 框架应用：\n```javascript\n'use strict'\n\nconst Koa = require('koa')\n\nconst app = new Koa()\n\napp.use(ctx=\u003e{\n\tctx.body = 'hello world'\n})\n\napp.listen(80)\n```\n只要改动2处，就可以迁移至 foi ：\n（1）变更引用模块\n```javascript\nconst Koa = require('koa')\n```\n变更为\n```javascript\nconst Foi = require('foi')\n```\n\n\n（2）变更初始化方式\n```javascript\nconst app = new Koa()\n```\n变更为\n```javascript\nconst app = new Foi({event, context, callback})\n```\n\n\n总共只需改动以上 2 行代码，以下部分为 Serverless 附加的代码：\n```javascript\nmodule.exports.handler = function(event, context, callback){\n\n}\n```\n\n\n\n\n### 中间件兼容情况\nfoi 兼容 Koa 框架的绝大多数中间件，但以下2类中间件不支持：\n\n\n1. 中间件承载的功能已经封装在 foi 框架里面了，例如 koa-bodyparser 中间件，可以直接通过 ctx.request.body  获取 body 请求的 body 内容。\n1. 基于“流”的中间件，更准确地讲，foi 不支持基于 Koa 的 req 和 res 对象的绝大多数中间件。（在 foi 实现中，屏蔽的原生的 req 和 res，虽然仍然能够使用 req 和 res，但这只是作为 request 和 response 的别名）\n\n\n\n## 框架 foi 和 midway比较\nmidway 是阿里云自研的 Node.js Web 开发框架，能够实现 foi 的所有功能。两者的差异非常大，举一个例子，如果说 foi 类比于 jQuery 的话，可以将 midway 类比为 Angular + Typescript + Webpack + Node.js + NPM。\n\n\nfoi 仅作为一个非浸入式开发的工具库，而 midway 提供了用于 Serverless 开发的一整套工具链。\n\n\n以下是 foi 和 midway 的对比：\n\n|  | foi | midway |\n| :---: | :---: | :---: |\n| 统一对接各云平台接口 | 支持 | 支持 |\n| 提供和 Koa 一致的 API | 支持 | 支持 |\n| 学习成本 | 非常低 | 非常高 |\n| 自动化部署 | 不支持 | 支持 |\n| 开发工具链 | 无 | 丰富 |\n\n\n\n## 支持情况\n各云平台触发器的支持情况：\n\n| 云平台 | 触发器 | 触发器名称 | 支持情况 |\n| :---: | :---: | :---: | :---: |\n| 阿里云 | API网关触发器 | aliyun-apigw | 已支持 |\n| 阿里云 | HTTP触发器 | aliyun-http | 开发中 |\n| 腾讯云 | API网关触发器 | qcloud-apigw | 开发中 |\n| 华为云 | API网关触发器 | huawei-apigw | 开发中 |\n| 百度智能云 | API网关触发器 | baidu-apigw | 开发中 |\n| AWS | API网关触发器 | aws-apigw | 开发中 |\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finlym%2Ffoi","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Finlym%2Ffoi","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Finlym%2Ffoi/lists"}