Javascript 节点模块导入出现意外令牌错误

Javascript 节点模块导入出现意外令牌错误,javascript,node.js,azure,azure-web-app-service,azure-webapps,Javascript,Node.js,Azure,Azure Web App Service,Azure Webapps,我有一个使用WebSocket lib()的节点应用程序,我可以在本地机器上运行这个应用程序 但是,当我将其发布到Azure应用程序服务时,我得到了相同代码的以下错误 我已经检查过这两个版本是否都运行相同版本的node 12.13.0,我已经完成了npm安装,并且软件包看起来是相同的(无论如何,发布时也会包括它们)。下面的错误实际上是抱怨来自ws模块的文件 触发此错误的代码行是:var-WebSocket=require('ws') Wed Dec 04 2019 13:03:04 GMT+00

我有一个使用WebSocket lib()的节点应用程序,我可以在本地机器上运行这个应用程序

但是,当我将其发布到Azure应用程序服务时,我得到了相同代码的以下错误

我已经检查过这两个版本是否都运行相同版本的node 12.13.0,我已经完成了
npm安装
,并且软件包看起来是相同的(无论如何,发布时也会包括它们)。下面的错误实际上是抱怨来自
ws
模块的文件

触发此错误的代码行是:
var-WebSocket=require('ws')

Wed Dec 04 2019 13:03:04 GMT+0000(协调世界时):应用程序引发了未捕获的异常并被终止:
SyntaxError:意外标记{
在exports.runInThisContext(vm.js:53:16)
在模块处编译(Module.js:373:25)
在Object.Module.\u extensions..js(Module.js:416:10)
在Module.load(Module.js:343:32)
在Function.Module.\u加载(Module.js:300:12)
at Module.require(Module.js:353:17)
根据需要(内部/module.js:12:17)
在对象上。(D:\home\site\wwwroot\node\u modules\ws\index.js:3:19)
在模块处编译(Module.js:409:26)
在Object.Module.\u extensions..js(Module.js:416:10)
应用程序引发了未捕获的异常并被终止:
SyntaxError:意外标记{
在exports.runInThisContext(vm.js:53:16)
在模块处编译(Module.js:373:25)
在Object.Module.\u extensions..js(Module.js:416:10)
在Module.load(Module.js:343:32)
在Function.Module.\u加载(Module.js:300:12)
at Module.require(Module.js:353:17)
根据需要(内部/module.js:12:17)
在对象上。(D:\home\site\wwwroot\node\u modules\ws\index.js:3:19)
在模块处编译(Module.js:409:26)
在Object.Module.\u extensions..js(Module.js:416:10)

作为参考,我尝试让GitHub repo的示例在本地和Azure应用程序服务上工作

首先,我将其所有源代码下载到本地机器上,并在我的本地目录、我的本地文件结构中运行
npm install
npm install ws
,如下图所示

图1.示例
服务器stats
的本地文件结构

其次,我尝试直接使用命令
node index.js
运行它,但遇到了一个问题
错误:找不到模块“....”
,然后我更改了代码的文件
index.js
,如下所示

const WebSocket = require('ws'); // The original content is `require('../..')`
然后,我运行了
node index.js
并打开浏览器查看结果,效果很好

图2.
server stats
在本地服务器上运行良好

正如我所知,Azure应用程序服务要求节点应用程序绑定code
process.env.port
中的端口值,并使用
web.config
文件启动,因此我更改了
index.js
文件,并创建了一个
web.config
文件,其中的内容来自Kudu wiki页面,并使用
index.js
,而不是
server.js
,代码如下

  • index.js

    const port =  8080;
    
    server.listen(port, function() {
      console.log('Listening on http://localhost:'+port);
    });
    
  • web.config

    <?xml version="1.0" encoding="utf-8"?>
    <!--
         This configuration file is required if iisnode is used to run node processes behind
         IIS or IIS Express.  For more information, visit:
    
         https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config
    -->
    
    <configuration>
      <system.webServer>
        <!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support -->
        <webSocket enabled="false" />
        <handlers>
          <!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module -->
          <add name="iisnode" path="index.js" verb="*" modules="iisnode"/>
        </handlers>
        <rewrite>
          <rules>
            <!-- Do not interfere with requests for node-inspector debugging -->
            <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
              <match url="^index.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 site entry point -->
            <rule name="DynamicContent">
              <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
              </conditions>
              <action type="Rewrite" url="index.js"/>
            </rule>
          </rules>
        </rewrite>
    
        <!-- 'bin' directory has no special meaning in node.js and apps can be placed in it -->
        <security>
          <requestFiltering>
            <hiddenSegments>
              <remove segment="bin"/>
            </hiddenSegments>
          </requestFiltering>
        </security>
    
        <!-- Make sure error responses are left untouched -->
        <httpErrors existingResponse="PassThrough" />
    
        <!--
          You can control how Node is hosted within IIS using the following options:
            * watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server
            * node_env: will be propagated to node as NODE_ENV environment variable
            * debuggingEnabled - controls whether the built-in debugger is enabled
    
          See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options
        -->
        <!--<iisnode watchedFiles="web.config;*.js"/>-->
      </system.webServer>
    </configuration>
    
    
    

    在我为
    应用程序设置添加了一个新的recode
    网站\u节点\u默认\u版本
    12.13.0

    图4.通过
    应用程序设置

    我在浏览器中打开它,它确实起作用了

    图5.对于带有默认设置的websocket功能,它不起作用

    上述结果存在两个问题

  • 对于使用websocket的webapp,需要启用
    常规设置中的
    Web套接字
    功能

    图6.在
    常规设置中启用
    Web套接字
    功能

  • 默认情况下,Azure为HTTP访问启用了SSL,因此需要对
    public/index.html
    文件中的websocket url使用
    wss
    而不是
    ws

    图7.对于Azure应用程序服务上启用的SSL,在
    public/index.html
    中使用
    wss
    而不是
    ws

  • 最后,它可以在Azure应用程序服务上运行


    请发布您的代码/努力,以便我们能够帮助您。@Prabhjotsinghkaint我已经添加了导致错误的代码。我不知道是什么原因导致错误,所以没有尝试任何操作。我已经确保节点版本相同,并且安装了包。您是否使用“使用节点”设置了节点版本进入Azure?只需要求100%确保您在本地和服务器上拥有完全相同的节点版本server@WojciechDynus我通过在应用程序设置中添加
    WEBSITE\u node\u DEFAULT\u version
    键来设置节点版本,使其与在本地计算机上相同,并且当我在控制台和kudu控制台上运行
    node-v
    时,它会返回正确的版本。如果“使用节点”步骤是指创建新资源时,那么我从ASP.NET上运行的express template创建了一个。我需要启用web套接字,并且在节点堆栈web app中没有该选项。
    <?xml version="1.0" encoding="utf-8"?>
    <!--
         This configuration file is required if iisnode is used to run node processes behind
         IIS or IIS Express.  For more information, visit:
    
         https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config
    -->
    
    <configuration>
      <system.webServer>
        <!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support -->
        <webSocket enabled="false" />
        <handlers>
          <!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module -->
          <add name="iisnode" path="index.js" verb="*" modules="iisnode"/>
        </handlers>
        <rewrite>
          <rules>
            <!-- Do not interfere with requests for node-inspector debugging -->
            <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
              <match url="^index.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 site entry point -->
            <rule name="DynamicContent">
              <conditions>
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
              </conditions>
              <action type="Rewrite" url="index.js"/>
            </rule>
          </rules>
        </rewrite>
    
        <!-- 'bin' directory has no special meaning in node.js and apps can be placed in it -->
        <security>
          <requestFiltering>
            <hiddenSegments>
              <remove segment="bin"/>
            </hiddenSegments>
          </requestFiltering>
        </security>
    
        <!-- Make sure error responses are left untouched -->
        <httpErrors existingResponse="PassThrough" />
    
        <!--
          You can control how Node is hosted within IIS using the following options:
            * watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server
            * node_env: will be propagated to node as NODE_ENV environment variable
            * debuggingEnabled - controls whether the built-in debugger is enabled
    
          See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options
        -->
        <!--<iisnode watchedFiles="web.config;*.js"/>-->
      </system.webServer>
    </configuration>