繁体   English   中英

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

[英]Deploying NodeJS Express to Azure web app via continuous integration, get EADDRINUSE error

我有一个在本地运行良好的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编辑器中运行它时,server.listen(port)行将抛出此错误:错误:监听EADDRINUSE \\。\\ pipe \\ e2d786c0-80d3-4948- 92dd-47267c1d84d2

显然,这表明该端口上已经有其他设备在运行。

如果我尝试通过单击App Service编辑器工具栏中的“运行”按钮来运行它,则会收到另一个错误:

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');

是什么原因造成的? 我应该如何排除故障?

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

我必须添加基本的Web.config和iisnode.yml文件,然后它才能工作。 我将文件放在下面,以防万一有人需要它们。 值得注意的是,Azure使用的Node的默认版本为0.0.something-or-other,您必须通过Kudu控制台手动查看其可用的版本,然后在iisnode.yml中进行指定,如下所述: https:/ /blogs.msdn.microsoft.com/azureossds/2016/04/20/nodejs-and-npm-versions-on-azure-app-services/

无论如何,这些文件:

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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM