如何使用简单的HttpGET-CRUD-NodeJS示例?

时间:2016-10-02 21:48:27

标签: node.js azure-functions

鉴于我只使用生成的默认模板:

module.exports = function (context, req, intable) {
    context.log("Retrieved records:", intable);
    context.res = {
        status: 200,
        body: intable
    };
    context.done();
};

和以下json文件:

{
  "bindings": [
    {
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get"
      ],
      "authLevel": "function"
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    },
    {
      "type": "table",
      "name": "inTable",
      "tableName": "person",
      "connection": "serverlessexamplestorage_STORAGE",
      "direction": "in",
      "take": "100"
    }
  ],
  "disabled": false
}

如何成功调用此功能?

enter image description here

1 个答案:

答案 0 :(得分:3)

门户网站" Run"按钮的工作原理是向您的函数发送POST请求。但是,该模板指定methods: [ "get" ]限制函数仅支持GET请求(因此405"方法不允许"错误)。

您可以使用Postman等客户端或您喜欢的客户端发送GET请求,该功能将成功运行。或者,您也可以通过添加" post"来允许该函数接受POST请求。方法数组(methods: [ "get", "post" ]),你可以从门户网站调用它。

我同意这有点令人困惑。问题是函数门户不是一个完整的HTTP客户端,因此它不允许您指定http方法,标题等。我们的仓库中有一个open issue来改进它。我们在门户网站中构建功能齐全的HTTP客户端的程度在多大程度上是TBD,所以现在最好的选择是为所有简单情况使用外部客户端。

相关问题