拦截器
本节介绍云函数拦截器,当请求云函数时会先执行拦截器,可以通过拦截器对云函数的请求加以控制。
本节目录
创建拦截器
拦截器也是一个云函数,创建一个云函数并且命名为 __interceptor__
即创建了拦截器云函数。
TIP
__interceptor__
为固定命名,其它名称无效。
下面是一个简单的拦截器示例,与其它云函数不同的是,拦截器云函数接受两个参数,第二个参数 next
为一个回调函数。
typescript
export default async function (ctx: FunctionContext, next: Function) {
const password = ctx.query.password
if (password === '123456') {
return await next(ctx)
}
return '禁止访问'
}
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
拦截指定 IP 的请求
typescript
export default async function (ctx: FunctionContext, next: Function) {
const ip = ctx.headers['x-forwarded-for']
// 拦截指定的 IP 地址
if(ip === '111.111.111.111') {
ctx.response.status(403).send('禁止访问')
return
}
// 继续执行后面的云函数
return await next(ctx)
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
拦截未认证的请求
typescript
export default async function (ctx: FunctionContext, next: Function) {
if(ctx.user) {
return await next(ctx)
}
ctx.response.status(403).send('禁止访问')
}
1
2
3
4
5
6
7
2
3
4
5
6
7