简体   繁体   English

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

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

I have an Express app running fine locally, whose entry point is index.js, which looks like this: 我有一个在本地运行良好的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);

I have a package.json file which has this section: 我有一个package.json文件,其中包含以下部分:

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

I've set up continuous deployment from Gitlab into an Azure Web App. 我已经设置了从Gitlab到Azure Web App的连续部署。

When I try to run it from the Azure App Service Editor with npm run start, or npm run dev, the server.listen(port) line throws me this error: Error: listen EADDRINUSE \\.\\pipe\\e2d786c0-80d3-4948-92dd-47267c1d84d2 当我尝试使用npm run start或npm run dev从Azure App Service编辑器中运行它时,server.listen(port)行将抛出此错误:错误:监听EADDRINUSE \\。\\ pipe \\ e2d786c0-80d3-4948- 92dd-47267c1d84d2

Obviously, this suggests something else is already running on the port. 显然,这表明该端口上已经有其他设备在运行。

If I try to run it by clicking the "Run" button in the App Service Editor toolbar, I get a different error: 如果我尝试通过单击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)

Line 6 of index.js is: index.js的第6行是:

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

What could be causing this? 是什么原因造成的? How should I troubleshoot? 我应该如何排除故障?

Have you tried defining a differet port to use? 您是否尝试过定义要使用的差异端口? In Azure, there's an app settings or properties page where you can define environment variables. 在Azure中,有一个应用程序设置或属性页,您可以在其中定义环境变量。 You can define your port there, change it to a couple of different ports, and see what happens. 您可以在此处定义端口,将其更改为几个不同的端口,然后看看会发生什么。

I had to add pretty rudimentary Web.config and iisnode.yml files, and then it worked. 我必须添加基本的Web.config和iisnode.yml文件,然后它才能工作。 I'll put my files below, just in case anyone needs them. 我将文件放在下面,以防万一有人需要它们。 Notably, the default version of Node used by Azure is 0.0.something-or-other, and you have to manually see which versions it has available via the Kudu console, then specify in the iisnode.yml, as described here: https://blogs.msdn.microsoft.com/azureossds/2016/04/20/nodejs-and-npm-versions-on-azure-app-services/ 值得注意的是,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/

Anyway, the files: 无论如何,这些文件:

iisnode.yml: iisnode.yml:

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

Web.config: 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