跳到主要内容

通过 HTTP 访问云托管

通过 HTTP 访问服务,您可以将云托管服务配置为标准的 HTTP 接口,实现灵活的域名和路径管理。本文档介绍如何配置云托管的 HTTP 访问以及如何使用自定义路径。

前置条件

  • 已创建云开发环境
  • 已部署至少一个云托管服务

配置 HTTP 访问

步骤 1:创建域名关联

  1. 前往 云开发平台 - HTTP 访问服务
  2. 在「域名关联资源」模块点击「新建」按钮
  3. 配置以下信息:
配置项说明示例
关联资源类型选择「云托管」,然后选择目标云托管服务my-service
域名选择默认域名、自定义域名或 *(匹配所有域名)默认域名
触发路径设置 HTTP 访问路径,支持 / 或自定义路径/helloworld

💡 提示:生产环境建议绑定已备案的自定义域名,以获得完整的服务能力。配置方法请参考 自定义域名配置

步骤 2:测试访问

配置完成后,访问 域名 + 触发路径 即可调用云托管服务:

# 示例:访问默认域名的云托管服务
curl https://your-env-id.service.tcloudbase.com/helloworld

发起 HTTP 请求

配置完成后,可以使用任何 HTTP 客户端访问云托管服务。

# GET 请求
curl https://your-domain/helloworld

# POST 请求
curl -X POST https://your-domain/helloworld \
-H "Content-Type: application/json" \
-d '{"name": "张三", "email": "zhangsan@example.com"}'

# PUT 请求
curl -X PUT https://your-domain/helloworld/123 \
-H "Content-Type: application/json" \
-d '{"name": "李四"}'

# DELETE 请求
curl -X DELETE https://your-domain/helloworld/123

云托管服务开发

接收 HTTP 请求

云托管服务是标准的 HTTP 服务,可以使用任何 Web 框架接收请求:

const express = require('express');
const app = express();

// 解析 JSON 请求体
app.use(express.json());

// GET 请求
app.get('/helloworld', (req, res) => {
res.json({
users: [
{ id: 1, name: '张三' },
{ id: 2, name: '李四' }
]
});
});

// POST 请求
app.post('/helloworld', (req, res) => {
const { name, email } = req.body;

res.json({
success: true,
user: { id: 3, name, email }
});
});

// 启动服务
const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`服务运行在端口 ${PORT}`);
});

RESTful API 示例

const express = require('express');
const app = express();

app.use(express.json());

// 模拟数据库
let users = [
{ id: 1, name: '张三', email: 'zhangsan@example.com' },
{ id: 2, name: '李四', email: 'lisi@example.com' }
];

// 获取所有用户
app.get('/helloworld', (req, res) => {
res.json({ success: true, users });
});

// 获取单个用户
app.get('/helloworld/:id', (req, res) => {
const user = users.find(u => u.id === parseInt(req.params.id));

if (!user) {
return res.status(404).json({
success: false,
error: 'User not found'
});
}

res.json({ success: true, user });
});

// 创建用户
app.post('/helloworld', (req, res) => {
const { name, email } = req.body;

const newUser = {
id: users.length + 1,
name,
email
};

users.push(newUser);
res.status(201).json({ success: true, user: newUser });
});

// 更新用户
app.put('/helloworld/:id', (req, res) => {
const user = users.find(u => u.id === parseInt(req.params.id));

if (!user) {
return res.status(404).json({
success: false,
error: 'User not found'
});
}

user.name = req.body.name || user.name;
user.email = req.body.email || user.email;

res.json({ success: true, user });
});

// 删除用户
app.delete('/helloworld/:id', (req, res) => {
const index = users.findIndex(u => u.id === parseInt(req.params.id));

if (index === -1) {
return res.status(404).json({
success: false,
error: 'User not found'
});
}

users.splice(index, 1);
res.json({ success: true, message: 'User deleted' });
});

const PORT = process.env.PORT || 8080;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});

相关文档