{"id":13498577,"url":"https://github.com/serverless-components/tencent-express","last_synced_at":"2025-04-04T15:32:06.530Z","repository":{"id":54840081,"uuid":"217311344","full_name":"serverless-components/tencent-express","owner":"serverless-components","description":"Easily deploy serverless Express.js applications to Tencent Cloud with the Serverless Framework","archived":false,"fork":false,"pushed_at":"2021-03-22T08:18:42.000Z","size":1226,"stargazers_count":103,"open_issues_count":4,"forks_count":15,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-03-20T15:01:33.103Z","etag":null,"topics":["serverless","serverless-components","serverless-framework","tencent","tencent-cloud"],"latest_commit_sha":null,"homepage":"","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/serverless-components.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-10-24T13:52:29.000Z","updated_at":"2024-02-23T02:36:40.000Z","dependencies_parsed_at":"2022-08-14T04:20:52.738Z","dependency_job_id":null,"html_url":"https://github.com/serverless-components/tencent-express","commit_stats":null,"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serverless-components%2Ftencent-express","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serverless-components%2Ftencent-express/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serverless-components%2Ftencent-express/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/serverless-components%2Ftencent-express/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/serverless-components","download_url":"https://codeload.github.com/serverless-components/tencent-express/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247202999,"owners_count":20900889,"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":["serverless","serverless-components","serverless-framework","tencent","tencent-cloud"],"created_at":"2024-07-31T21:00:37.061Z","updated_at":"2025-04-04T15:32:01.524Z","avatar_url":"https://github.com/serverless-components.png","language":"JavaScript","readme":"⚠️⚠️⚠️ 所有框架组件项目迁移到 [tencent-framework-components](https://github.com/serverless-components/tencent-framework-components).\n\n[![Serverless Components](https://img.serverlesscloud.cn/2020210/1581352135771-express.png)](http://serverless.com)\n\n\u003cbr/\u003e\n\n**腾讯云 Express 组件** ⎯⎯⎯ 通过使用 [Tencent Serverless Framework](https://github.com/serverless/components/tree/cloud)，基于云上 Serverless 服务（如网关、云函数等），实现“0”配置，便捷开发，极速部署你的 Express 应用，Express 组件支持丰富的配置扩展，提供了目前最易用、低成本并且弹性伸缩的 Express 项目开发/托管能力。\n\u003cbr/\u003e\n\n特性介绍：\n\n- [x] **按需付费** - 按照请求的使用量进行收费，没有请求时无需付费\n- [x] **\"0\"配置** - 只需要关心项目代码，之后部署即可，Serverless Framework 会搞定所有配置。\n- [x] **极速部署** - 仅需几秒，部署你的整个 Express 应用。\n- [x] **实时日志** - 通过实时日志的输出查看业务状态，便于直接在云端开发应用。\n- [x] **云端调试** - 针对 Node.js 框架支持一键云端调试能力，屏蔽本地环境的差异。\n- [x] **便捷协作** - 通过云端的状态信息和部署日志，方便的进行多人协作开发。\n- [x] **自定义域名** - 支持配置自定义域名及 HTTPS 访问\n\n\u003cbr/\u003e\n\n快速开始：\n\n1. [**安装**](#1-安装)\n2. [**创建**](#2-创建)\n3. [**部署**](#3-部署)\n4. [**配置**](#4-配置)\n5. [**开发调试**](#5-开发调试)\n6. [**查看状态**](#6-查看状态)\n7. [**移除**](#7-移除)\n\n更多资源：\n\n- [**架构说明**](#架构说明)\n- [**账号配置**](#账号配置)\n\n\u0026nbsp;\n\n### 1. 安装\n\n通过 npm 安装最新版本的 Serverless Framework\n\n```bash\n$ npm install -g serverless\n```\n\n### 2. 创建\n\n通过如下命令和模板链接，快速创建一个 Express 应用：\n\n```bash\n$ serverless init express-starter --name example\n$ cd example\n```\n\n### 3. 部署\n\n在 `serverless.yml` 文件所在的项目根目录，运行以下指令进行部署：\n\n```bash\n$ serverless deploy\n```\n\n部署时需要进行身份验证，如您的账号未 [登陆](https://cloud.tencent.com/login) 或 [注册](https://cloud.tencent.com/register) 腾讯云，您可以直接通过 `微信` 扫描命令行中的二维码进行授权登陆和注册。\n\n\u003e 注意: 如果希望查看更多部署过程的信息，可以通过`serverless deploy --debug` 命令查看部署过程中的实时日志信息。\n\n部署完成后，控制台会打印相关的输出信息，您可以通过 `${output:${stage}:${app}:apigw.url}` 的形式在其他 `serverless` 组件中引用该组件的 API 网关访问链接（或通过类似的形式引用该组建其他输出结果），具体的，可以查看完成的输出文档：\n\n- [点击此处查看输出文档](https://github.com/serverless-components/tencent-express/tree/master/docs/output.md)\n\n### 4. 配置\n\nExpress 组件支持 0 配置部署，也就是可以直接通过配置文件中的默认值进行部署。但你依然可以修改更多可选配置来进一步开发该 Express 项目。\n\n以下是 Express 组件的 `serverless.yml`配置示例：\n\n```yml\n# serverless.yml\n\ncomponent: express # (required) name of the component. In that case, it's express.\nname: expressDemo # (required) name of your express component instance.\norg: orgDemo # (optional) serverless dashboard org. default is the first org you created during signup.\napp: appDemo # (optional) serverless dashboard app. default is the same as the name property.\nstage: dev # (optional) serverless dashboard stage. default is dev.\n\ninputs:\n  src:\n    src: ./ # (optional) path to the source folder. default is a hello world app.\n    exclude:\n      - .env\n  functionName: expressDemo\n  region: ap-guangzhou\n  runtime: Nodejs10.15\n  apigatewayConf:\n    protocols:\n      - http\n      - https\n    environment: release\n```\n\n点此查看[全量配置及配置说明](https://github.com/serverless-components/tencent-express/tree/master/docs/configure.md)\n\n当你根据该配置文件更新配置字段后，再次运行 `serverless deploy` 或者 `serverless` 就可以更新配置到云端。\n\n### 5. 开发调试\n\n部署了 Express.js 应用后，可以通过开发调试能力对该项目进行二次开发，从而开发一个生产应用。在本地修改和更新代码后，不需要每次都运行 `serverless deploy` 命令来反复部署。你可以直接通过 `serverless dev` 命令对本地代码的改动进行检测和自动上传。\n\n可以通过在 `serverless.yml`文件所在的目录下运行 `serverless dev` 命令开启开发调试能力。\n\n`serverless dev` 同时支持实时输出云端日志，每次部署完毕后，对项目进行访问，即可在命令行中实时输出调用日志，便于查看业务情况和排障。\n\n除了实时日志输出之外，针对 Node.js 应用，当前也支持云端调试能力。在开启 `serverless dev` 命令之后，将会自动监听远端端口，并将函数的超时时间临时配置为 900s。此时你可以通过访问 chrome://inspect/#devices 查找远端的调试路径，并直接对云端代码进行断点等调试。在调试模式结束后，需要再次部署从而将代码更新并将超时时间设置为原来的值。详情参考[开发模式和云端调试](https://cloud.tencent.com/document/product/1154/43220)。\n\n### 6. 查看状态\n\n在`serverless.yml`文件所在的目录下，通过如下命令查看部署状态：\n\n```\n$ serverless info\n```\n\n### 7. 移除\n\n在`serverless.yml`文件所在的目录下，通过以下命令移除部署的 Express 服务。移除后该组件会对应删除云上部署时所创建的所有相关资源。\n\n```\n$ serverless remove\n```\n\n和部署类似，支持通过 `serverless remove --debug` 命令查看移除过程中的实时日志信息。\n\n## 架构说明\n\nExpress 组件将在腾讯云账户中使用到如下 Serverless 服务：\n\n- [x] **API 网关** - API 网关将会接收外部请求并且转发到 SCF 云函数中。\n- [x] **SCF 云函数** - 云函数将承载 Express.js 应用。\n- [x] **CAM 访问控制** - 该组件会创建默认 CAM 角色用于授权访问关联资源。\n- [x] **COS 对象存储** - 为确保上传速度和质量，云函数压缩并上传代码时，会默认将代码包存储在特定命名的 COS 桶中。\n- [x] **SSL 证书服务** - 如果你在 yaml 文件中配置了 `apigatewayConf.customDomains` 字段，需要做自定义域名绑定并开启 HTTPS 时，也会用到证书管理服务和域名服务。Serverless Framework 会根据已经备案的域名自动申请并配置 SSL 证书。\n\n## 账号配置\n\n当前默认支持 CLI 扫描二维码登录，如您希望配置持久的环境变量/秘钥信息，也可以本地创建 `.env` 文件\n\n```console\n$ touch .env # 腾讯云的配置信息\n```\n\n在 `.env` 文件中配置腾讯云的 SecretId 和 SecretKey 信息并保存\n\n如果没有腾讯云账号，可以在此[注册新账号](https://cloud.tencent.com/register)。\n\n如果已有腾讯云账号，可以在[API 密钥管理](https://console.cloud.tencent.com/cam/capi)中获取 `SecretId` 和`SecretKey`.\n\n```\n# .env\nTENCENT_SECRET_ID=123\nTENCENT_SECRET_KEY=123\n```\n\n## 静态资源服务\n\n如果想要支持返回静态资源，比如图片之类的，需要在入口文件 `sls.js` 中指定相关 `MIME` 类型的文件为二进制，这样云函数在返回请求结果给 API 网关是，会对指定类型进行 `Base64` 编码，最终返回给客户端才能正常显示。如下：\n\n```js\nconst express = require('express')\nconst app = express()\n\n// Routes\n// ...\n\napp.binaryTypes = ['*/*']\n\nmodule.exports = app\n```\n\n`['*/*']` 代表所有文件类型将进行 `Base64` 编码，如果需要定制化，可以配置为 `['image/png']`，意思是指定 `png` 格式的图片类型。\n\n更多文件类型的 `MIME` 类型，可参考 [mime-db](https://github.com/jshttp/mime-db/blob/master/db.json)。\n\n### slsInitialize 应用初始化\n\n有些时候，Express 服务在启动前，需要进行一个初始化操作，比如数据库建连，就可以通过在 Express 实例对象上添加 `slsInitialize` 函数来实现，如下：\n\n```js\nconst express = require('express')\nconst mysql = require('mysql2/promise')\n\nconst app = new express()\n\n// ...\n\napp.slsInitialize = async () =\u003e {\n  app.db = await mysql.createConnection({\n    host: 'localhost',\n    user: 'root',\n    database: 'test'\n  })\n}\n\n// don't forget to export!\nmodule.exports = app\n```\n\n这样应用部署到云函数后，在函数服务逻辑执行前，会先执行 `slsInitialize()` 函数，来初始化数据库连接。\n\n## License\n\nMIT License\n\nCopyright (c) 2020 Tencent Cloud, Inc.\n","funding_links":[],"categories":["组件"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserverless-components%2Ftencent-express","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fserverless-components%2Ftencent-express","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fserverless-components%2Ftencent-express/lists"}