|
| 1 | +[](http://serverless.com) |
| 2 | + |
| 3 | +# 腾讯云 Koa 组件 |
| 4 | + |
| 5 | +简体中文 | [English](https://github.com/serverless-components/tencent-koa/tree/master/README.en.md) |
| 6 | + |
| 7 | +## 简介 |
| 8 | + |
| 9 | +使用腾讯云 Koa 组件,可快速的在腾讯云创建,配置和管理一个 [Koa 框架](https://koajs.com/) 服务。 |
| 10 | + |
| 11 | +## 目录 |
| 12 | + |
| 13 | +1. [安装](#1-安装) |
| 14 | +2. [创建](#2-创建) |
| 15 | +3. [配置](#3-配置) |
| 16 | +4. [部署](#4-部署) |
| 17 | +5. [移除](#5-移除) |
| 18 | + |
| 19 | +### 1. 安装 |
| 20 | + |
| 21 | +通过 npm 安装 serverless |
| 22 | + |
| 23 | +```bash |
| 24 | +$ npm install -g serverless |
| 25 | +``` |
| 26 | + |
| 27 | +### 2. 创建 |
| 28 | + |
| 29 | +本地创建 `serverless.yml` 文件: |
| 30 | + |
| 31 | +```bash |
| 32 | +$ touch serverless.yml |
| 33 | +``` |
| 34 | + |
| 35 | +初始化一个新的 npm 包,并安装 koa: |
| 36 | + |
| 37 | +```bash |
| 38 | +# 创建后持续回车 |
| 39 | +$ npm init |
| 40 | + |
| 41 | +# 安装 koa |
| 42 | +$ npm i --save koa |
| 43 | +``` |
| 44 | + |
| 45 | +创建一个 `sls.js`文件,并在其中创建您的 koa App: |
| 46 | + |
| 47 | +```bash |
| 48 | +$ touch sls.js |
| 49 | +``` |
| 50 | + |
| 51 | +```js |
| 52 | +const koa = require('koa') |
| 53 | +const app = new koa() |
| 54 | + |
| 55 | +app.use(async (ctx, next) => { |
| 56 | + if (ctx.path !== '/') return next() |
| 57 | + ctx.body = 'Hello from Koa' |
| 58 | +}) |
| 59 | + |
| 60 | +// set binary types |
| 61 | +// app.binaryTypes = [*/*]; |
| 62 | + |
| 63 | +// don't forget to export! |
| 64 | +module.exports = app |
| 65 | +``` |
| 66 | + |
| 67 | +### 3. 配置 |
| 68 | + |
| 69 | +在 serverless.yml 中进行如下配置 |
| 70 | + |
| 71 | +```yml |
| 72 | +# serverless.yml |
| 73 | + |
| 74 | +org: orgDemo # (optional) serverless dashboard org. default is the first org you created during signup. |
| 75 | +app: appDemo # (optional) serverless dashboard app. default is the same as the name property. |
| 76 | +stage: dev # (optional) serverless dashboard stage. default is dev. |
| 77 | +component: koa # (required) name of the component. In that case, it's koa. |
| 78 | +name: koaDemo # (required) name of your koa component instance. |
| 79 | + |
| 80 | +inputs: |
| 81 | + src: |
| 82 | + src: ./ # (optional) path to the source folder. default is a hello world app. |
| 83 | + exclude: |
| 84 | + - .env |
| 85 | + region: ap-guangzhou |
| 86 | + runtime: Nodejs10.15 |
| 87 | + apigatewayConf: |
| 88 | + protocols: |
| 89 | + - http |
| 90 | + - https |
| 91 | + environment: release |
| 92 | +``` |
| 93 | +
|
| 94 | +- [点击此处查看配置文档](https://github.com/serverless-components/tencent-koa/tree/master/docs/configure.md) |
| 95 | +
|
| 96 | +### 4. 部署 |
| 97 | +
|
| 98 | +如您的账号未[登陆](https://cloud.tencent.com/login)或[注册](https://cloud.tencent.com/register)腾讯云,您可以直接通过`微信`扫描命令行中的二维码进行授权登陆和注册。 |
| 99 | + |
| 100 | +通过`sls`命令进行部署,并可以添加`--debug`参数查看部署过程中的信息 |
| 101 | + |
| 102 | +> 注:`sls`命令是`serverless`命令的缩写 |
| 103 | + |
| 104 | +``` |
| 105 | +$ sls deploy |
| 106 | +``` |
| 107 | +
|
| 108 | +部署完毕后,可以在浏览器中访问部署成功地 url。 |
| 109 | +
|
| 110 | +### 5. 移除 |
| 111 | +
|
| 112 | +通过以下命令移除部署的 Koa 服务。 |
| 113 | +
|
| 114 | +``` |
| 115 | +$ sls remove |
| 116 | +``` |
| 117 | +
|
| 118 | +### 账号配置(可选) |
| 119 | +
|
| 120 | +当前默认支持 CLI 扫描二维码登录,如您希望配置持久的环境变量/秘钥信息,也可以本地创建 `.env` 文件 |
| 121 | +
|
| 122 | +```bash |
| 123 | +$ touch .env # 腾讯云的配置信息 |
| 124 | +``` |
| 125 | + |
| 126 | +在 `.env` 文件中配置腾讯云的 SecretId 和 SecretKey 信息并保存 |
| 127 | + |
| 128 | +如果没有腾讯云账号,可以在此[注册新账号](https://cloud.tencent.com/register)。 |
| 129 | + |
| 130 | +如果已有腾讯云账号,可以在[API 密钥管理](https://bash.cloud.tencent.com/cam/capi)中获取 `SecretId` 和`SecretKey`. |
| 131 | + |
| 132 | +``` |
| 133 | +# .env |
| 134 | +TENCENT_SECRET_ID=123 |
| 135 | +TENCENT_SECRET_KEY=123 |
| 136 | +``` |
| 137 | + |
| 138 | +### 还支持哪些组件? |
| 139 | + |
| 140 | +可以在 [Serverless Components](https://github.com/serverless/components) repo 中查询更多组件的信息。 |
| 141 | + |
| 142 | +## License |
| 143 | + |
| 144 | +MIT License |
| 145 | + |
| 146 | +Copyright (c) 2020 Tencent Cloud, Inc. |
0 commit comments