【问题标题】:ArangoDB Foxx Microservices Getting Started tutorial: what is a working URI for this example?ArangoDB Foxx 微服务入门教程:此示例的有效 URI 是什么?
【发布时间】:2023-03-31 03:46:01
【问题描述】:

这里的教程Getting started · ArangoDB v3.4.0 Documentation使用这个代码:

// continued
router.post('/sum', function (req, res) {
const values = req.body.values;
res.send({
    result: values.reduce(function (a, b) {
    return a + b;
    }, 0)
});
})
.body(joi.object({
values: joi.array().items(joi.number().required()).required()
}).required(), 'Values to add together.')
.response(joi.object({
result: joi.number().required()
}).required(), 'Sum of the input values.')
.summary('Add up numbers')
.description('Calculates the sum of an array of number values.');

提供预期参数(两个数字)的 URI 示例是什么?

【问题讨论】:

    标签: arangodb arangodb-foxx


    【解决方案1】:

    假设您的服务器实例通过 HTTP 在localhost:8529 上运行,数据库为_system,Foxx 服务的挂载点为/getting-started,那么/sum 端点的URL 为:

    http://localhost:8529/getting-started/sum

    请注意,数据库_system 是特殊的:它是默认值,这意味着您不必显式指定它。以下 URL 是等效的:

    http://localhost:8529/_db/_system/getting-started/sum

    如果 Foxx 服务挂载在另一个数据库中,则将 _system 替换为实际数据库的名称。

    /sum 是一个 POST 路由 (router.post(...)),预期的主体(HTTP 请求的内容/负载)由 joi 模式描述:一个 JSON 对象,属性名称为 values,属性值为 a数字数组(一个或多个数字)。

    使用 Curl,您可以像这样查询服务:

    curl --data "{\"values\":[5,6]}" http://localhost:8529/getting-started/sum

    (请求方法-X POST由Curl推断)

    响应是一个 JSON 对象,其属性键为 result,计算出的数字作为属性值:

    {"result":11}

    如果您尝试在浏览器中访问 URL,它将是一个 GET 请求(没有负载)并失败并出现 HTTP 错误:405 Method Not Allowed

    【讨论】:

    • 谢谢。卷曲工作。这个示例如何适应在浏览器中运行,同时保持超级简单(在本基础教程的级别上)?我想我应该问一个新问题,但我不知道该怎么问。为什么本教程没有提到这条路线在浏览器中不起作用?除了我之外的每个人都已经知道了吗?大声笑
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多