通过持续集成将NodeJS Express部署到Azure Web应用程序,获取EADDRINUSE错误

时间:2017-10-16 12:21:12

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

我有一个在本地运行正常的Express应用程序,其入口点是index.js,如下所示:

const express = require('express');
const http = require('http');
const bodyParser = require('body-parser');
const morgan = require('morgan');
const app = express();
const router = require('./router');
const mongoose = require('mongoose');
const cors = require('cors');
try{
const config = require('./config');
}
catch(e){
    console.log(e);
}
mongoose.connect(process.env.cosmosConn || config.conn);
//App setup
app.use(morgan('combined'));
app.use(cors());
app.use(bodyParser.json({ type: '*/*' }));
router(app);

//Server setup

const port = process.env.port || process.env.PORT || 3090;
const server = http.createServer(app);
server.listen(port);
console.log('Server listening on:', port);

我有一个包含此部分的package.json文件:

  "scripts": {
    "dev": "nodemon index.js",
    "start": "node index.js"
  }

我已经设置了从Gitlab到Azure Web App的持续部署。

当我尝试使用npm run start或npm run dev从Azure App Service Editor运行它时,server.listen(port)行会抛出此错误:     错误:听EADDRINUSE \。\ pipe \ e2d786c0-80d3-4948-92dd-47267c1d84d2

显然,这表明其他东西已经在端口上运行了。

如果我尝试通过单击"运行"来运行它。在App Service Editor工具栏中,我收到一个不同的错误:

Mon Oct 16 2017 12:15:55 GMT+0000 (Coordinated Universal Time): Application has thrown an uncaught exception and is terminated:
SyntaxError: Unexpected token ILLEGAL
    at Module._compile (module.js:434:25)
    at Object..js (module.js:464:10)
    at Module.load (module.js:353:31)
    at Function._load (module.js:311:12)
    at Module.require (module.js:359:17)
    at require (module.js:375:17)
    at Object.<anonymous> (D:\home\site\wwwroot\index.js:6:16)
    at Module._compile (module.js:446:26)
    at Object..js (module.js:464:10)
    at Module.load (module.js:353:31)

index.js的第6行是:

const router = require('./router');

可能导致这种情况的原因是什么?我该如何排除故障?

2 个答案:

答案 0 :(得分:0)

您是否尝试过定义要使用的差异端口?在Azure中,有一个应用程序设置或属性页面,您可以在其中定义环境变量。您可以在那里定义端口,将其更改为几个不同的端口,并查看会发生什么。

答案 1 :(得分:0)

我必须添加相当基本的Web.config和iisnode.yml文件,然后才能工作。我会把我的文件放在下面,以防万一有人需要它们。值得注意的是,Azure使用的Node的默认版本是0.0.something-or-other,你必须通过Kudu控制台手动查看它可用的版本,然后在iisnode.yml中指定,如下所述:{{3 }}

无论如何,文件:

iisnode.yml:

nodeProcessCommandLine: "D:\Program Files (x86)\nodejs\6.11.1\node.exe"
loggingEnabled: true
devErrorsEnabled: true

的Web.config:

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your Node.js application, please visit
  -->
<configuration>
  <appSettings>
    <!--
    <add key="StorageAccountName" value="" />
    <add key="StorageAccountKey" value="" />
    <add key="ServiceBusNamespace" value="" />
    <add key="ServiceBusIssuerName" value="" />
    <add key="ServiceBusIssuerSecretKey" value="" />
    -->
  </appSettings>
  <system.webServer>
    <!-- mimeMap enables IIS to serve particular file types as specified by fileExtension. -->
    <staticContent>
      <mimeMap fileExtension=".svg" mimeType="image/svg+xml" />
    </staticContent>

    <modules runAllManagedModulesForAllRequests="false" />

    <!-- Web.Debug.config adds attributes to this to enable remote debugging when publishing in Debug configuration. -->
      <!--<iisnode watchedFiles="web.config;*.js;routes\*.js;views\*.pug"/>-->

    <!-- Remote debugging (Azure Website with git deploy): Comment out iisnode above, and uncomment iisnode below. -->
    <iisnode watchedFiles="web.config;*.js;routes\*.js;views\*.pug"
      loggingEnabled="true"
      devErrorsEnabled="true"
      nodeProcessCommandLine="node.exe &#45;&#45;debug"/>

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

      <!-- Remote debugging (Azure Website with git deploy): Uncomment NtvsDebugProxy handler below.
      Additionally copy Microsoft.NodejsTools.WebRole to 'bin' from the Remote Debug Proxy folder.-->
      <add name="NtvsDebugProxy" path="ntvs-debug-proxy/0bcce65a-4ec7-46fd-bb0a-cacb9307bf84" verb="*" resourceType="Unspecified"
        type="Microsoft.NodejsTools.Debugger.WebSocketProxy, Microsoft.NodejsTools.WebRole"/>
    </handlers>

    <security>
      <requestFiltering>
        <hiddenSegments>
          <remove segment="bin" />
        </hiddenSegments>
      </requestFiltering>
    </security>

    <rewrite>
      <rules>
        <clear />
        <!-- Remote debugging (Azure Website with git deploy): Uncomment the NtvsDebugProxy rule below. --> 
        <rule name="NtvsDebugProxy" enabled="true" stopProcessing="true"> 
          <match url="^ntvs-debug-proxy/.*"/> 
        </rule>

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

        <rule name="app" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
          <match url="iisnode.+" negate="true" />
          <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
          <action type="Rewrite" url="index.js" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

  <!-- Remote debugging (Azure Website with git deploy): uncomment system.web below --> 
  <system.web> 
    <httpRuntime targetFramework="4.5"/> 
    <customErrors mode="Off"/> 
  </system.web> 
</configuration>