对象解构不在Azure中工作

时间:2016-07-25 03:35:02

标签: node.js azure

我有一个简单的函数,它在Azure webjob中运行,并包含一个我正在使用ES2016解构的语句。该函数运行时,我看到以下错误:

SyntaxError: Unexpected token {

这是我的功能:

// Formats an address object into a string
formatAddress(address) {

    if (address) {
        const { street, city, state, zipcode } = address;
        return `${street} ${city}, ${state} ${zipcode}`;
    }
}

我在Node 6.3.0上运行我的Azure网站。

删除解构修复了问题。在我自己的机器(OS X)上本地运行,一切都按预期进行解析。

在Windows版本的Node上解析不可用,还是我必须启用标志?

谢谢!

更新

  //package.json
  "engines": {
    "node": "6.3.x"
  }

# iisnode.yml
debuggingEnabled: false
loggingEnabled: true
nodeProcessCommandLine: "D:\home\site\wwwroot\bin\node.exe" --use_strict // points to node 6.3.1 binary

1 个答案:

答案 0 :(得分:0)

如何在Azure Web Apps上设置Node.js版本?

请尝试转到Azure protal(https://ms.portal.azure.com),然后将WEBSITE_NODE_DEFAULT_VERSION更改为6.3.0部分中的Application settings。修改后,它在我身边工作正常。

enter image description here

更新

这是我在Azure Web Apps上的整个测试项目,目录结构是:

app.js
iisnode.yml
package.json
web.config

每个文件的内容也很简单:

app.js

var http = require("http");

var address = {street : "street", city : "city", state:"state", zipcode:"zipcode"};
const { street, city, state, zipcode } = address;
var server = http.createServer(function(request, response) {
  response.writeHead(200, {"Content-Type": "text/html"});
  response.write(`${street} ${city}, ${state} ${zipcode}`);
  response.end();
});

server.listen(process.env.PORT || 1337);

iisnode.yml

debuggingEnabled: false
loggingEnabled: true

package.json

{   
    "engines": {
        "node": "6.3.x"
      }
}

web.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
     <system.webServer>
          <webSocket enabled="false" />
          <handlers>
               <!-- Indicates that the app.js file is a node.js site to be handled by the iisnode module -->
               <add name="iisnode" path="app.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="^app.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="app.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" />
          <iisnode watchedFiles="web.config;*.js" debuggingEnabled="false" />
     </system.webServer>
</configuration>

并且只有一个Applition设置:WEBSITE_NODE_DEFAULT_VERSION6.3.0。 测试项目在我身边很好用。希望它会对你有所帮助。