laf/docs/guide/function/interceptor.md
nightwhite 75ba39a3f6
doc: optimize cloud function documents and database documents (#1076)
* doc: optimize cloud function documents and database documents

* doc: fix eq link

* doc: change database policy position

* doc: small adjustment

* doc: optimize automatic compilation

* doc: optimize homepage copy and top buttons

* doc: add ignore docs Address
2023-04-25 12:14:19 +08:00

769 B
Raw Blame History

title
云函数拦截器

{{ $frontmatter.title }}

如果需要使用拦截器,需要创建一个云函数并且命名为 __interceptor__的云函数。

::: info __interceptor__ 为固定命名 :::

Laf云函数拦截器是在所有的云函数请求之前被请求故而也可以叫做前置拦截器。

只有拦截器的返回值为 true ,才会去请求的原本的云函数

下面是一个简单的拦截器示例,如果IP是111.111.111.111,则可以继续访问原本的云函数

export async function main(ctx: FunctionContext) {
  // 获取请求的实际IP
  const ip = ctx.headers['x-real-ip']
  if(ip == '111.111.111.111'){
    return true
  }else{
    return false
  }
}

更多用途可自由发挥!