简体   繁体   中英

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:

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:

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

I've set up continuous deployment from Gitlab into an 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

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:

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:

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. 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. 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/

Anyway, the files:

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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