Skip to content

Commit

Permalink
fix: support koa server
Browse files Browse the repository at this point in the history
  • Loading branch information
yugasun committed Sep 25, 2020
1 parent c4c5e4e commit fd37c09
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 178 deletions.
170 changes: 0 additions & 170 deletions README.en.md

This file was deleted.

47 changes: 41 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,26 +169,30 @@ Next.js 组件将在腾讯云账户中使用到如下 Serverless 服务:

## 更多组件

可以在 [Serverless Components](https://github.com/serverless/components) repo 中查询更多组件的信息
可以在 [Serverless Components](https://github.com/serverless/components) 仓库中查询更多组件的信息

## 项目迁移 - 自定义 express 服务
## 项目迁移

如果项目使用了自定义 Node.js 服务,比如 express 或者 koa,你需要做如下改造工作。

### 自定义 express 服务

如果你的 Next.js 项目本身运行就是基于 `express` 自定义服务的,那么你需要在项目中自定义入口文件 `sls.js`,需要参考你的服务启动文件进行修改,以下是一个模板文件:

```js
const express = require('express')
const next = require('next')

const app = next({ dev: false })
const handle = app.getRequestHandler()

// not report route for custom monitor
const noReportRoutes = ['/_next', '/static', '/favicon.ico']

async function createServer() {
const app = next({ dev: false })
const handle = app.getRequestHandler()

await app.prepare()
const server = express()

const server = express()
server.all('*', (req, res) => {
noReportRoutes.forEach((route) => {
if (req.path.indexOf(route) !== -1) {
Expand All @@ -208,6 +212,37 @@ async function createServer() {
module.exports = createServer
```

### 自定义 koa 服务

如果你的项目使用的是 Koa 作为 Node.js 服务,需要在项目中自定义入口文件 `sls.js`,需要参考你的服务启动文件进行修改,以下是一个模板文件:

```js
const Koa = require('koa')
const next = require('next')

async function createServer() {
const app = next({ dev: false })
const handle = app.getRequestHandler()

const server = new Koa()
server.use((ctx) => {
ctx.status = 200
ctx.respond = false
ctx.req.ctx = ctx

return handle(ctx.req, ctx.res)
})

// define binary type for response
// if includes, will return base64 encoded, very useful for images
server.binaryTypes = ['*/*']

return server
}

module.exports = createServer
```

## 自定义监控

当在部署 Next.js 应用时,如果 `serverless.yml` 中未指定 `role`,默认会尝试绑定 `QCS_SCFExcuteRole`,并且开启自定义监控,帮助用户收集应用监控指标。对于为自定义入口文件的项目,会默认上报除含有 `/_next``/static``/favicon.ico` 的路由。如果你想自定义上报自己的路由性能,那么可以自定义 `sls.js` 入口文件,对于无需上报的路由,在 express 服务的 `req` 对象上添加 `__SLS_NO_REPORT__` 属性值为 `true` 即可。比如:
Expand Down
6 changes: 5 additions & 1 deletion src/_shims/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ module.exports.handler = async (event, context) => {
app.request.__SLS_CONTEXT__ = context

if (!server) {
server = createServer(app, null, app.binaryTypes || [])
server = createServer(
app.callback && typeof app.callback === 'function' ? app.callback() : app,
null,
app.binaryTypes || []
)
}

context.callbackWaitsForEmptyEventLoop =
Expand Down
2 changes: 1 addition & 1 deletion src/_shims/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
"dependencies": {
"express": "^4.17.1",
"tencent-component-monitor": "^1.1.0",
"tencent-serverless-http": "^1.2.0"
"tencent-serverless-http": "^1.3.1"
}
}

0 comments on commit fd37c09

Please sign in to comment.