如何在node.js azure web app中加载html依赖项

时间:2018-01-12 17:39:54

标签: html node.js azure azure-web-sites

作为免责声明,我对javascript和Azure都很新。我的目标是构建一个Tableau Web数据连接器。我有以下脚本。

var http = require('http');
http.createServer((function() {
    // Create the connector object
    var myConnector = tableau.makeConnector();

    // Define the schema
    myConnector.getSchema = function(schemaCallback) {
        var cols = [{
            id: "id",
            dataType: tableau.dataTypeEnum.int
        }, {
            id: "link",
            dataType: tableau.dataTypeEnum.string
        }, {
            id: "likes",
            dataType: tableau.dataTypeEnum.int
        }];

        var tableSchema = {
            id: "Facebook",
            alias: "real-time likes",
            columns: cols
        };

        schemaCallback([tableSchema]);
    };

    // Download the data
    myConnector.getData = function(table, doneCallback) {
        var username = 'xxx'; 
        var password = 'xxx'; 
        var url = 'xxxx';
        var xhttp = new XMLHttpRequest();
        xhttp.open('POST', url, true);

        xhttp.onreadystatechange = function() {//Call a function when the state changes.
            if(xhttp.readyState == 4 && xhttp.status == 200) {
                var response = JSON.parse(xhttp.responseText)
                tableData = [];

                tableData.push({
                    "id": response.id,
                    "link": response.link,
                    "likes": response.likes,
                    });

                table.appendRows(tableData);
                doneCallback();
                }
            }

        params = username + ';' + password
        xhttp.send(params)
        };

    tableau.registerConnector(myConnector);

    // Create event listeners for when the user submits the form
    $(document).ready(function() {
        $("#submitButton").click(function() {
            tableau.connectionName = "Facebook Likes"; // This will be the data source name in Tableau
            tableau.submit(); // This sends the connector object to Tableau
        });
    });
})).listen(process.env.PORT || 1337);

{
  "name": "azure-facebook-likes",
  "author": "Xander",
  "version": "1.0",
  "description": "application for use with Tableau web data connector to return facebook likes",
  "tags": [
    "facebook",
    "tableau"
  ],
  "license": "MIT",
  "scripts": {
    "start": "node FacebookLikes.js"
  }
}

我在本地构建并测试了html和javascript,一切都按预期工作。我从开发版本中添加的唯一内容是createServer调用。如果我根据Azure文档执行git push,则推送成功执行,我可以在“部署选项”下看到我的应用程序中列出的部署;但是,当我打开网页时,我收到消息,“您无权查看此目录或页面。”我的研究引导我包括package.json和web.config文件,如下所示。

<configuration>
    <system.webServer>

        <handlers>
            <!-- indicates that the app.js file is a node.js application to be handled by the iisnode module -->
            <add name="iisnode" path="FacebookLikes.js" verb="*" modules="iisnode" />
        </handlers>

        <rewrite>
            <rules>
                <!-- Don't interfere with requests for logs -->
                <rule name="LogFile" patternSyntax="ECMAScript" stopProcessing="true">
                    <match url="^[a-zA-Z0-9_\-]+\.js\.logs\/\d+\.txt$" />
                </rule>

                <!-- Don't interfere with requests for node-inspector debugging -->
                <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">                    
                    <match url="^FacebookLikes.js\/debug[\/]?" />
                </rule>

                <!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
                <rule name="StaticContent">
                    <action type="Rewrite" url="public{REQUEST_URI}" />
                </rule>

                <!-- All other URLs are mapped to the Node.js application entry point -->
                <rule name="DynamicContent">
                    <conditions>
                        <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True" />
                    </conditions>
                    <action type="Rewrite" url="FacebookLikes.js" />
                </rule>
            </rules>
        </rewrite>

    </system.webServer>

    <security>
        <ipSecurity allowUnlisted="true"> 
    </security>

</configuration>

-

"scripts": {
    "start": "node FacebookLikes.js"
}

使用此配置,当我浏览到网页时,我收到消息“由于发生内部服务器错误,无法显示页面”。 Logging-errors.txt显示以下内容:“应用程序抛出了未捕获的异常并终止:ReferenceError:未定义tableau。” tableau定义来自html文件中的connectors.tableau.com依赖项,但是我可以告诉我,当我指定

And

在package.json文件中,html文件中的脚本未加载。我已经在这方面工作了几天而且没有想法。有没有人建议如何加载依赖项?或者有人可以把我推向正确的方向,看看我应该注意什么?任何见解将不胜感激。

谢谢!

2 个答案:

答案 0 :(得分:1)

您使用的代码应该从浏览器运行而不是从Node.js运行。您可以将代码从FacebookLikes.js移动到HTML文件,如下所示:

<html>
<head>
    <title>Facebook Likes</title>
    <meta http-equiv="Cache-Control" content="no-store" />

    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js" type="text/javascript"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>

    <script src="https://connectors.tableau.com/libs/tableauwdc-2.3.latest.js" type="text/javascript"></script>
    <script src="FacebookLikes.js" type="text/javascript"></script>

    <script>
        $(document).ready(function() {
            $("#submitButton").click(function() {
                tableau.connectionName = "Facebook Likes"; // This will be the data source name in Tableau
                tableau.submit(); // This sends the connector object to Tableau
            });
        });
    </script>
</head>

<body>
    <div class="container container-table">
        <div class="row vertical-center-row">
            <div class="text-center col-md-4 col-md-offset-4">
                <button type = "button" id = "submitButton" class = "btn btn-success" style = "margin: 10px;">Get Facebook Likes!</button>
            </div>
        </div>
    </div>
</body>

<script>

    var myConnector = tableau.makeConnector();

    // Define the schema
    myConnector.getSchema = function(schemaCallback) {
        // ...
    };

    // Download the data
    myConnector.getData = function(table, doneCallback) {
        // ...
    };

    tableau.registerConnector(myConnector);

</script>

</html>

然后删除FacebookLikes.jsweb.configpackage.json文件。

答案 1 :(得分:0)

Azure在应用程序设置下有一个默认文档列表。我将我的html文件命名为FacebookLikes.html,这并不是一个默认的文档名称。我重命名了我的文件index.html,删除了web.config和package.json文件,并从javascript代码中删除了创建服务器调用(并添加了一个trailing()),并且Web应用程序按预期运行。可以想象我可以将FacebookLikes.html添加到默认文档中,它应该可以工作。