【问题标题】:Debugging a Node.js Azure function调试 Node.js Azure 函数
【发布时间】:2017-10-16 18:33:31
【问题描述】:

您好,我正在尝试在本地调试和开发一个简单的 JavaScript timerTrigger 函数 azure 应用程序,但我遇到了问题。我正在尝试遵循本教程:https://docs.microsoft.com/en-us/azure/azure-functions/functions-run-local

调查资料

请提供以下信息:

  • 时间戳:
  • 功能应用名称:testcenter123
  • 函数名称(视情况而定):noderepo
  • 调用 ID:
  • 地区:
重现步骤

我通过 Git 存储库处理了该应用程序,因此它也在我的本地计算机上。

提供重现问题所需的步骤:

  1. 转到函数应用的根级别并运行(通过 powershell):func host start --debug vscode

    λ func host start --debug vscode
    
                  %%%%%%
                 %%%%%%
            @   %%%%%%    @
          @@   %%%%%%      @@
       @@@    %%%%%%%%%%%    @@@
     @@      %%%%%%%%%%        @@
       @@         %%%%       @@
         @@      %%%       @@
           @@    %%      @@
                %%
                %
    
    [10/16/2017 6:54:48 PM] Host has been specialized
    Listening on http://localhost:7071/
    Hit CTRL-C to exit...
    [10/16/2017 6:54:48 PM] Reading host configuration file 'C:\Users\xxxxxxxx\code\test-repos\NodeRepo1\host.json'
    [10/16/2017 6:54:48 PM] Host configuration file read:
    [10/16/2017 6:54:48 PM] { }
    [10/16/2017 6:54:48 PM] Loaded custom extension 'BotFrameworkConfiguration'
    [10/16/2017 6:54:48 PM] Loaded custom extension 'SendGridConfiguration'
    [10/16/2017 6:54:48 PM] Loaded custom extension 'EventGridExtensionConfig'
    [10/16/2017 6:54:48 PM] Generating 0 job function(s)
    [10/16/2017 6:54:48 PM] Starting Host (HostId=laptop2062-1189224982, Version=1.0.11232.0, ProcessId=8180, Debug=False, Attempt=0)
    [10/16/2017 6:54:48 PM] No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. config.UseServiceBus(), config.UseTimers(), etc.).
    [10/16/2017 6:54:48 PM] Job host started
    [10/16/2017 6:54:48 PM] Executing HTTP request: {
    [10/16/2017 6:54:48 PM]   "requestId": "b2d45bf0-1947-4ad7-a9cd-cdfa469d04d2",
    [10/16/2017 6:54:48 PM]   "method": "GET",
    [10/16/2017 6:54:48 PM]   "uri": "/"
    [10/16/2017 6:54:48 PM] }
    [10/16/2017 6:54:48 PM] Executed HTTP request: {
    [10/16/2017 6:54:48 PM]   "requestId": "b2d45bf0-1947-4ad7-a9cd-cdfa469d04d2",
    [10/16/2017 6:54:48 PM]   "method": "GET",
    [10/16/2017 6:54:48 PM]   "uri": "/",
    [10/16/2017 6:54:48 PM]   "authorizationLevel": "Anonymous"
    [10/16/2017 6:54:48 PM] }
    [10/16/2017 6:54:48 PM] Response details: {
    [10/16/2017 6:54:48 PM]   "requestId": "b2d45bf0-1947-4ad7-a9cd-cdfa469d04d2",
    [10/16/2017 6:54:48 PM]   "status": "OK"
    [10/16/2017 6:54:48 PM] }
    launch.json for VSCode configured.
    Debugger listening on [::]:5858
    [10/16/2017 6:54:51 PM] Executing HTTP request: {
    [10/16/2017 6:54:51 PM]   "requestId": "9fe12a9e-446a-4c8e-90db-d0004ac42e31",
    [10/16/2017 6:54:51 PM]   "method": "GET",
    [10/16/2017 6:54:51 PM]   "uri": "/admin/functions/noderepo"
    [10/16/2017 6:54:51 PM] }
    [10/16/2017 6:54:51 PM] Executed HTTP request: {
    [10/16/2017 6:54:51 PM]   "requestId": "9fe12a9e-446a-4c8e-90db-d0004ac42e31",
    [10/16/2017 6:54:51 PM]   "method": "GET",
    [10/16/2017 6:54:51 PM]   "uri": "/admin/functions/noderepo",
    [10/16/2017 6:54:51 PM]   "authorizationLevel": "Anonymous"
    [10/16/2017 6:54:51 PM] }
    [10/16/2017 6:54:51 PM] Response details: {
    [10/16/2017 6:54:51 PM]   "requestId": "9fe12a9e-446a-4c8e-90db-d0004ac42e31",
    [10/16/2017 6:54:51 PM]   "status": "MethodNotAllowed"
    [10/16/2017 6:54:51 PM] }
    
  2. 在浏览器中访问http://localhost:7071/admin/functions/noderepo

预期行为

查看计时器触发器后,我得到:

<Error>
<Message>
The requested resource does not support http method 'GET'.
</Message>
</Error>

我尝试在 URL 中用 timerTriggermyTrigger 替换 noderepo,但还是一样。我该如何调试?

实际行为

执行 JavaScript 函数方法并在 VS Code 上中断

其他信息

我的index.js 是这个:

module.exports = function (context, myTimer) {
    var timeStamp = new Date().toISOString();

    config.UseTimers();

    if(myTimer.isPastDue)
    {
        context.log('JavaScript is running late!');
    }
    context.log('JavaScript timer trigger function ran!', timeStamp);

    context.done();
};

很简单....

【问题讨论】:

标签: javascript node.js azure azure-functions


【解决方案1】:

您是否尝试过运行新版本的 Azure 工具 cli?

npm install azure-functions-core-tools@core

新版本的 cli 构建在 .NET 核心之上,并且是 x 平台。 该架构不再依赖 Edge.js,而是将处理任务卸载到节点工作程序。 cli worker 总是以 --inspect 开头,这应该允许您使用 VSCode 或 chrome 轻松调试,您只需要确保您的 launch.json 配置正确并匹配地址和端口。

{
     "type": "node",
     "request": "attach",
     "name": "Attach to Remote",
     "address": "localhost",
     "port": 9229, // Or 5858 
     "sourceMaps": true,
     "localRoot": "${workspaceRoot}"
}

此外,如果需要,这应该允许您升级到节点 > 6.5.0,但需要注意的是,我相信您需要更改 Azure 函数中的运行时才能启用它。

【讨论】:

  • 哦,我明白了!使用 cli 工具而不是 Azure 上的 UI?所以,如果我通过 CLI 进行设置,然后执行 pubish,我将拥有一个新的 function azure app,其中包含类似计时器触发器和 powershell 之类的东西?
  • 没错,您可以使用 cli 工具将您的函数从您的机器发布到使用 func azure functionapp publish functionName 的 azure 函数
【解决方案2】:

您似乎在 host.json 附近没有函数文件夹。您能否确认您有以下文件夹:C:\Users\xxxxxx\code\test-repos\NodeRepo1\{yourFunctionN‌​ame}?如果不使用创建它:

func new -l Javascript -t TimerTrigger -name myTimer

之后,您应该会看到: 您应该在启动日志中看到类似:

[10/16/2017 11:05:15 PM] Found the following functions: 
[10/16/2017 11:05:15 PM] Host.Functions.myTimer

【讨论】:

  • 嗨,你能帮我一个忙,把我的名字删掉在\Users之后吗?谢谢。无论如何,我有我的noderepo1,我想要那个 不是我的功能应用程序。无论如何,我认为最好只使用 CLI 来发布。因为我确实有一个host.json,其中有{}。问题虽然......你能告诉我你用什么吗?因为我尝试了您的func new,然后我尝试了func host start,但仍然出现同样的错误。
【解决方案3】:

这里的答案更清楚一些。我也没有发现Microsoft's Article 很清楚,而是Alexey Rodionov's answer 设置了正确的路线。

假设您要将端点引用为http://localhost:7071/api/MyFunction

目录结构应如下所示:

  • MyFunction 必须是目录的名称
  • 文件 function.json 和 index.js 必须在该目录中。

以下两个文件位于您的解决方案的根目录中。

  • host.json
  • local.settings.json

我发现 Microsoft 的文章中缺少的是您必须在步骤之间更改目录这一事实。

func init Sample
cd Sample
func new --language JavaScript --template "Http Trigger" --name MyFunction

` 请注意,您在文章中指定“Http Trigger”的方式也不正确。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    • 2019-03-29
    • 2020-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-21
    相关资源
    最近更新 更多