{"id":28102272,"url":"https://github.com/serverless-devs/fc-http","last_synced_at":"2025-05-13T19:49:03.376Z","repository":{"id":41986943,"uuid":"399718555","full_name":"Serverless-Devs/fc-http","owner":"Serverless-Devs","description":"在 阿里云 函数计算 中使用您现有的中间件框架（例如 Express、Koa)","archived":false,"fork":false,"pushed_at":"2023-11-23T08:44:37.000Z","size":361,"stargazers_count":11,"open_issues_count":0,"forks_count":2,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-14T08:46:32.073Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Serverless-Devs.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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":"2021-08-25T06:48:23.000Z","updated_at":"2023-11-05T00:25:22.000Z","dependencies_parsed_at":"2023-11-23T09:44:07.891Z","dependency_job_id":null,"html_url":"https://github.com/Serverless-Devs/fc-http","commit_stats":{"total_commits":45,"total_committers":6,"mean_commits":7.5,"dds":0.5111111111111111,"last_synced_commit":"e715ed95da0d961fc03d0e98b9aa96c8b0f36f30"},"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Serverless-Devs%2Ffc-http","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Serverless-Devs%2Ffc-http/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Serverless-Devs%2Ffc-http/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Serverless-Devs%2Ffc-http/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Serverless-Devs","download_url":"https://codeload.github.com/Serverless-Devs/fc-http/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254015935,"owners_count":21999960,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":[],"created_at":"2025-05-13T19:49:02.665Z","updated_at":"2025-05-13T19:49:03.366Z","avatar_url":"https://github.com/Serverless-Devs.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003e 注意⚠️，fc-http 暂时 不支持FC3.0。\n# fc-http\n\u003cp align=\"center\" class=\"flex justify-center\"\u003e\n  \u003ca href=\"https://nodejs.org/en/\" class=\"ml-1\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/node-%3E%3D%2010.8.0-brightgreen\" alt=\"node.js version\"\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/devsapp/website-fc/blob/master/LICENSE\" class=\"ml-1\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/License-MIT-green\" alt=\"license\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n此模块可以方便的将传统的 web 框架使用 [Nodejs runtime](https://help.aliyun.com/document_detail/58011.html) 的形式运行在阿里云函数计算。\n\n- [快速开始](#快速开始)\n  - [为什么要使用原生运行环境](#为什么要使用原生运行环境)\n  - [框架支持](#框架支持)\n  - [快速体验](#快速体验)\n    - [基本示例](#基本示例)\n    - [request详解](#request详解)\n    - [高级选项](#高级选项)\n  - [示例代码](https://github.com/devsapp/start-web-framework/tree/master/web-framework/nodejs/nodejs-runtime)\n- [关于我们](#关于我们)\n\n## 快速开始\n### 为什么要使用原生运行环境\n将nodejs应用部署在函数计算FC上，一般有下面几种方式\n- [原生Nodejs运行环境](https://help.aliyun.com/document_detail/58011.html)\n- [Custom Runtime](https://help.aliyun.com/document_detail/132044.html)\n- [Custom Container](https://help.aliyun.com/document_detail/179368.html)\n\n#### Nodejs Runtime\nNodejs运行环境（简称`Nodejs Runtime`）支持Nodejs6到Nodejs14的版本，用于只需要提供入口函数如下：\n```\nfunction(request, response, context) {}\n```\n当有请求触发的时候，函数计算平台会调用这个方法，启动您的应用进行处理，参考[示例](https://github.com/devsapp/start-fc/tree/master/http-function/fc-http-node.js14/src)\n\n#### Custom Runtime\n`Custom Runtime`是自定义运行环境，您可以自定义应用的启动脚本`bootstrap`进行启动web服务器。很方便的将您的应用快速迁移到函数计算平台，参考[示例](https://github.com/devsapp/start-web-framework/blob/master/web-framework/nodejs/express/src/code/bootstrap)\n\n#### Custom Container\n使用`Custom Container`方式，开发者需要提前准备好镜像。这样能够实现最大的灵活性。\n\n\u003e 从灵活性来看 `Custom Container` \u003e `Custom Runtime` \u003e `Nodejs Runtime`\n\n\u003e 从性能来看 `Nodejs Runtime` \u003e `Custom Runtime` \u003e `Custom Container`\n\n而且 `Nodejs Runtime`还有以下优点：\n- 版本支持到`nodejs14`, `Custom Runtime`默认环境是`nodejs10`,否则需要将nodejs的二进制包，上传到当前的运行环境\n- 日志输出友好，相比`Custom Runtime`在`高级查询`更具有可读性\n- 支持[layer](https://help.aliyun.com/document_detail/193057.html)层，提取公共依赖。\n\n\n## 框架支持\nFC-http目前已经支持主流Nodejs框架快速接入，并提供模版示例。通过执行指令 如：`s init express-app`快速体验。\n\n- Express: `s init express-app`\n- Koa: `s init koa-app`\n- Hapi: `s init hapi-app`\n- Egg: `s init egg-app`\n- nest: `s init nest-app`\n- nuxt: `s init nuxt-app`\n- thinkjs: `s init thinkjs-app`\n- Connect: `s init connect-app`\n- Sails\n- Fastify\n- Restify\n- Polka\n- Loopback\n\n\n## 快速体验\n```\n$ npm i @serverless-devs/fc-http\n```\n### 基本示例\n- 基本示例\n```\nconst serverless = require('@serverless-devs/fc-http');\nconst express = require('express')\nconst app = express()\n\napp.get('/user', (req, res) =\u003e {\n  res.send('hello user!')\n})\n\napp.get('/', (req, res) =\u003e {\n  res.send('Hello World!')\n})\n\nexports.handler = serverless(app)\n```\n更多例子见 `examples`\n\n### request详解\n下面例子中，`req`参数可以获取一些有用的信息\n```\napp.get('/user', (req, res) =\u003e {\n  res.send('hello user!')\n})\n```\n| 字段名 | 类型 | 示例 | 描述\n| --- | --- | --- | --- |\n| method   | String |  GET    |  HTTP请求方法    |\n| path   | String |  /api    |  HTTP请求方法    |\n| headers   | Object类型\t |  {'content-type': 'text/plain','x-forwarded-proto': 'https' }    |  存放来自HTTP客户端的键值对。|\n| queries   | Object类型\t |  {name:'age'}    |  存放来自HTTP路径中的查询部分的键值对，值的类型可以为字符串或数组。|\n| clientIP   | String类型\t |  '42.120.75.232'    |  客户端的IP地址。|\n| fcRequest   | Object类型\t |  [详情参考](https://help.aliyun.com/document_detail/74757.html#section-960-nx8-b4i)    |  FC原始的 request参数|\n| fcContext   | Object类型\t |  [详情参考](https://help.aliyun.com/document_detail/156876.htm?#section-mcp-kix-v2j)   | FC原始的 context参数|\n\n\n## 高级选项\n\nfc-http 接受第二个参数options，它可以配置：\n\n### 基本路径\n- basePath：无服务器应用程序的基本路径/挂载点。如果您希望使用多个 Lambda 来表示应用程序的不同部分，则很有用。\n\n- 配置前\n```\napp.get(\"/new\", (ctx) =\u003e {\n  res.send('Hello World!')\n})\n\nexports.handler = serverless(app);\n```\n```\n[GET] http://localhost/api/new -\u003e 404\n```\n\n- 配置后\n\n```\napp.get(\"/new\", (ctx) =\u003e {\n  res.send('Hello World!')\n})\n\nexports.handler = serverless(app, {\n  basePath: '/api'\n});\n```\n```\n[GET] http://localhost/api/new -\u003e 200\n```\n在 FC 中，上面的示例也适用于：\n```\nhttps://xxx.cn-hangzhou.fc.aliyuncs.com/2016-08-15/proxy/dk-http-demo/api/new -\u003e 200\n```\n\n### 转换\n- request：请求的转换，在它被发送到应用程序之前\n- response：响应的转换，在返回给 Lambda 之前\n\n转换是要分配的函数（req|res、事件、上下文）或对象。\n\n您可以在请求通过您的应用程序之前对其进行转换。\n\n您可以在响应返回之后，在发送之前对其进行转换：\n\n一些例子\n```\nexports.handler = serverless(app, {\n  request: {\n    key: 'value'\n  },\n  response(res) {\n    res.foo = 'bar';\n  }\n})\n\nexports.handler = serverless(app, {\n  request(request, event, context) {\n    request.context = event.requestContext;\n  },\n  async response(response, event, context) {\n    // the return value is always ignored\n    return new Promise(resolve =\u003e {\n      // override a property of the response, this will affect the response\n      response.statusCode = 420;\n\n      // delay your responses by 300ms!\n      setTimeout(() =\u003e {\n        // this value has no effect on the response\n        resolve('banana');\n      }, 300);\n    });\n  }\n})\n```\n\n### 二进制 Binary Mode\n\n二进制模式检测查看BINARY_CONTENT_TYPES环境变量，或者您可以指定类型数组，或自定义函数：\n\n```\n// turn off any attempt to base64 encode responses -- probably Not Going To Work At All\nserverless(app, {\n  binary: false\n});\n\n // this is the default, look at content-encoding vs. gzip, deflate and content-type against process.env.BINARY_CONTENT_TYPES\nserverless(app, {\n  binary: true\n});\n\n// set the types yourself - just like BINARY_CONTENT_TYPES but using an array you pass in, rather than an environment varaible\nserverless(app, {\n  binary: ['application/json', 'image/*']\n});\n\n// your own custom callback\nserverless(app, {\n  binary(headers) {\n    return ...\n  }\n});\n```\n\n# 关于我们\n- Serverless Devs 工具：\n    - 仓库：[https://www.github.com/serverless-devs/serverless-devs](https://www.github.com/serverless-devs/serverless-devs)    \n      \u003e 欢迎帮我们增加一个 :star2: \n    - 官网：[https://www.serverless-devs.com/](https://www.serverless-devs.com/)\n- 阿里云函数计算组件：\n    - 仓库：[https://github.com/devsapp/fc](https://github.com/devsapp/fc)\n    - 帮助文档：[https://www.serverless-devs.com/fc/readme](https://www.serverless-devs.com/fc/readme)\n- 钉钉交流群：33947367    \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserverless-devs%2Ffc-http","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fserverless-devs%2Ffc-http","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserverless-devs%2Ffc-http/lists"}