云函数快速入门
本节通过给出几个简单的云函数示例,快速展示云函数的能力和用法。
TIP
你可以在 https://laf.run
上创建一个应用,然后创建函数来调试这些云函数代码。
本节目录
Hello World
typescript
export default async function (ctx: FunctionContext) {
console.log('Hello World')
return 'hi, laf'
}
1
2
3
4
2
3
4
这个最简单的云函数,打印一条日志 Hello World
,并返回 hi, laf
做为其响应内容。 通过浏览器访问其地址即可看到 hi, laf
。
获取请求参数
typescript
export default async function (ctx: FunctionContext) {
// 获取请求参数
console.log(ctx.query)
// 获取客户端的 IP 地址
const ip = ctx.headers['x-forwarded-for']
return `你的 IP 地址是:${ip}`
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
响应 JSON 对象
typescript
export default async function (ctx: FunctionContext) {
// ...
return {
code: 'success',
message: '操作成功',
data: []
}
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
访问数据库
typescript
import cloud from '@lafjs/cloud'
const db = cloud.mongo.db
export default async function (ctx: FunctionContext) {
// 新增数据
await db.collection('users').insertOne({
username: 'laf',
created_at: new Date()
})
// 查询数据
const users = await db.collection('users')
.find()
.toArray()
return users
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
发起 HTTP 请求
typescript
export default async function (ctx: FunctionContext) {
const res = await fetch('https://laf.run/v1/regions')
const obj = await res.json()
return obj
}
1
2
3
4
5
2
3
4
5