0

My webapp works locally, but when I attempt to publish it on Azure Web Apps the following error pops up. It is showing Error: listen EADDRINUSE: address already in use :::8080, but the webapp should be using port 3000. I am new to creating webapp, so any help with be appreciated. I used visual studio code to publish the webapp following these settings - https://learn.microsoft.com/en-us/azure/developer/javascript/tutorial-vscode-azure-app-service-node-03. I chose linux and node 12 LTS.

Thank you!

enter image description here

#!/usr/bin/env node

/**
 * Module dependencies.
 */

var app = require('../app');
var debug = require('debug')('myapp:server');
var http = require('http');

/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
 * Create HTTP server.
 */

var server = http.createServer(app);

/**
 * Listen on provided port, on all network interfaces.
 */

server.listen(port);
server.on('error', onError);
server.on('listening', onListening);

/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}

/**
 * Event listener for HTTP server "error" event.
 */

function onError(error) {
  if (error.syscall !== 'listen') {
    throw error;
  }

  var bind = typeof port === 'string'
    ? 'Pipe ' + port
    : 'Port ' + port;

  // handle specific listen errors with friendly messages
  switch (error.code) {
    case 'EACCES':
      console.error(bind + ' requires elevated privileges');
      process.exit(1);
      break;
    case 'EADDRINUSE':
      console.error(bind + ' is already in use');
      process.exit(1);
      break;
    default:
      throw error;
  }
}

/**
 * Event listener for HTTP server "listening" event.
 */

function onListening() {
  var addr = server.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
}

/depl

2020-07-16T04:23:44.555179603Z   /  _  \ __________ _________   ____  
2020-07-16T04:23:44.555185003Z  /  /_\  \___   /  |  \_  __ \_/ __ \ 
2020-07-16T04:23:44.555189203Z /    |    \/    /|  |  /|  | \/\  ___/ 
2020-07-16T04:23:44.555193303Z \____|__  /_____ \____/ |__|    \___  >
2020-07-16T04:23:44.555197403Z         \/      \/                  \/ 
2020-07-16T04:23:44.555201303Z A P P   S E R V I C E   O N   L I N U X
2020-07-16T04:23:44.555205203Z 
2020-07-16T04:23:44.555208803Z Documentation: http://aka.ms/webapp-linux
2020-07-16T04:23:44.555212503Z NodeJS quickstart: https://aka.ms/node-qs
2020-07-16T04:23:44.555216303Z NodeJS Version : v12.16.3
2020-07-16T04:23:44.555220003Z Note: Any data outside '/home' is not persisted
2020-07-16T04:23:44.555223903Z 
2020-07-16T04:23:44.902776147Z Found build manifest file at '/home/site/wwwroot/oryx-manifest.toml'. Deserializing it...
2020-07-16T04:23:44.912621757Z Build Operation ID: |bLCEuDLkBSY=.df68a68e_
2020-07-16T04:23:46.477381888Z Writing output script to '/opt/startup/startup.sh'
2020-07-16T04:23:47.136900207Z Running #!/bin/sh
2020-07-16T04:23:47.137528808Z 
2020-07-16T04:23:47.137541508Z # Enter the source directory to make sure the script runs where the user expects
2020-07-16T04:23:47.137547108Z cd "/home/site/wwwroot"
2020-07-16T04:23:47.137551508Z 
2020-07-16T04:23:47.137555608Z export NODE_PATH=$(npm root --quiet -g):$NODE_PATH
2020-07-16T04:23:47.138703711Z if [ -z "$PORT" ]; then
2020-07-16T04:23:47.138716311Z         export PORT=8080
2020-07-16T04:23:47.138721811Z fi
2020-07-16T04:23:47.138725911Z 
2020-07-16T04:23:47.139711413Z echo Found tar.gz based node_modules.
2020-07-16T04:23:47.139723313Z extractionCommand="tar -xzf node_modules.tar.gz -C /node_modules"
2020-07-16T04:23:47.139728913Z echo "Removing existing modules directory from root..."
2020-07-16T04:23:47.139733513Z rm -fr /node_modules
2020-07-16T04:23:47.139737613Z mkdir -p /node_modules
2020-07-16T04:23:47.139741713Z echo Extracting modules...
2020-07-16T04:23:47.141077216Z $extractionCommand
2020-07-16T04:23:47.141089216Z export NODE_PATH="/node_modules":$NODE_PATH
2020-07-16T04:23:47.141094416Z export PATH=/node_modules/.bin:$PATH
2020-07-16T04:23:47.141098616Z if [ -d node_modules ]; then
2020-07-16T04:23:47.142105319Z     mv -f node_modules _del_node_modules || true
2020-07-16T04:23:47.142117019Z fi
2020-07-16T04:23:47.142122019Z 
2020-07-16T04:23:47.142126219Z if [ -d /node_modules ]; then
2020-07-16T04:23:47.142140519Z     ln -sfn /node_modules ./node_modules 
2020-07-16T04:23:47.142145419Z fi
2020-07-16T04:23:47.149224835Z 
2020-07-16T04:23:47.149263335Z echo "Done."
2020-07-16T04:23:47.149334335Z npm start
2020-07-16T04:23:48.155357951Z Found tar.gz based node_modules.
2020-07-16T04:23:48.155942252Z Removing existing modules directory from root...
2020-07-16T04:23:48.164530572Z Extracting modules...
2020-07-16T04:23:51.370781353Z Done.
2020-07-16T04:23:52.204989273Z npm info it worked if it ends with ok
2020-07-16T04:23:52.206185276Z npm info using npm@6.14.4
2020-07-16T04:23:52.206929677Z npm info using node@v12.16.3
2020-07-16T04:23:52.736398696Z npm info lifecycle myapp@0.0.0~prestart: myapp@0.0.0
2020-07-16T04:23:52.746327019Z npm info lifecycle myapp@0.0.0~start: myapp@0.0.0
2020-07-16T04:23:52.759340349Z 
2020-07-16T04:23:52.759356549Z > myapp@0.0.0 start /home/site/wwwroot
2020-07-16T04:23:52.759362549Z > node ./bin/www
2020-07-16T04:23:52.759366849Z 
2020-07-16T04:23:54.160267671Z Express server listening on port 8080
2020-07-16T04:23:54.170115279Z events.js:287
2020-07-16T04:23:54.170141079Z       throw er; // Unhandled 'error' event
2020-07-16T04:23:54.170147979Z       ^
2020-07-16T04:23:54.170152379Z 
2020-07-16T04:23:54.170156779Z Error: listen EADDRINUSE: address already in use :::8080
2020-07-16T04:23:54.170161379Z     at Server.setupListenHandle [as _listen2] (net.js:1313:16)
2020-07-16T04:23:54.170165779Z     at listenInCluster (net.js:1361:12)
2020-07-16T04:23:54.170170179Z     at Server.listen (net.js:1449:7)
2020-07-16T04:23:54.170174479Z     at module.exports (/home/site/wwwroot/routes/table_routes2.js:63:27)
2020-07-16T04:23:54.170178979Z     at Object. (/home/site/wwwroot/app.js:119:37)
2020-07-16T04:23:54.170183679Z     at Module._compile (internal/modules/cjs/loader.js:1133:30)
2020-07-16T04:23:54.170187979Z     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
2020-07-16T04:23:54.170192279Z     at Module.load (internal/modules/cjs/loader.js:977:32)
2020-07-16T04:23:54.170196679Z     at Function.Module._load (internal/modules/cjs/loader.js:877:14)
2020-07-16T04:23:54.170200879Z     at Module.require (internal/modules/cjs/loader.js:1019:19)
2020-07-16T04:23:54.170205179Z     at require (internal/modules/cjs/helpers.js:77:18)
2020-07-16T04:23:54.170209379Z     at Object. (/home/site/wwwroot/bin/www:7:11)
2020-07-16T04:23:54.170213779Z     at Module._compile (internal/modules/cjs/loader.js:1133:30)
2020-07-16T04:23:54.170218079Z     at Object.Module._extensions..js (internal/modules/cjs/loader.js:1153:10)
2020-07-16T04:23:54.170231979Z     at Module.load (internal/modules/cjs/loader.js:977:32)
2020-07-16T04:23:54.170236679Z     at Function.Module._load (internal/modules/cjs/loader.js:877:14)
2020-07-16T04:23:54.170240879Z Emitted 'error' event on Server instance at:
2020-07-16T04:23:54.170244979Z     at emitErrorNT (net.js:1340:8)
2020-07-16T04:23:54.170248979Z     at processTicksAndRejections (internal/process/task_queues.js:84:21) {
2020-07-16T04:23:54.170253279Z   code: 'EADDRINUSE',
2020-07-16T04:23:54.170257279Z   errno: 'EADDRINUSE',
2020-07-16T04:23:54.170261279Z   syscall: 'listen',
2020-07-16T04:23:54.170265279Z   address: '::',
2020-07-16T04:23:54.170269379Z   port: 8080
2020-07-16T04:23:54.170273379Z }
2020-07-16T04:23:54.198841600Z npm info lifecycle myapp@0.0.0~start: Failed to exec start script
2020-07-16T04:23:54.200937601Z npm ERR! code ELIFECYCLE
2020-07-16T04:23:54.201865802Z npm ERR! errno 1
2020-07-16T04:23:54.203466603Z npm ERR! myapp@0.0.0 start: `node ./bin/www`
2020-07-16T04:23:54.203976603Z npm ERR! Exit status 1
2020-07-16T04:23:54.204590604Z npm ERR! 
2020-07-16T04:23:54.205116404Z npm ERR! Failed at the myapp@0.0.0 start script.
2020-07-16T04:23:54.211395609Z npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
2020-07-16T04:23:54.346290903Z npm timing npm Completed in 2399ms
2020-07-16T04:23:54.347057103Z 
2020-07-16T04:23:54.347655604Z npm ERR! A complete log of this run can be found in:
2020-07-16T04:23:54.436761954Z npm ERR!     /root/.npm/_logs/2020-07-16T04_23_54_212Z-debug.log
williswin
  • 133
  • 13
  • I looked at the log, you first make sure that there are no port 8080 settings in your code. Then, it is recommended that `var port = process.env.PORT || 1337; server.listen(port);` for testing. Under normal circumstances, create a webapp according to the tutorial, the local port of node is 3000, 4200, 7000 and so on. When deploying, no additional configuration is required, because the azure node environment will automatically recognize it as a production environment, so 80 and 443 are used port. webapp only supports 80 and 443 ports. – Jason Pan Jul 17 '20 at 01:52
  • When I am running it locally it runs on port 3000. I tried changing it to 1337 locally also and it worked on 1337. I tried to deploy the web app live to 1337 and it is still showing as 8080. I can't find port 8080 anywhere in my code though. – williswin Jul 17 '20 at 15:44
  • Can you give me demo without sensitive info, and I will try. – Jason Pan Jul 17 '20 at 15:59
  • Here you go - https://github.com/wnguyen23/ProjectLane I'm also not able to connect to my database, sorry code is a mess. – williswin Jul 17 '20 at 16:52
  • Sorry for taking so long to reply to you, I have checked your demo carefully. I need to explain to you several aspects. – Jason Pan Jul 20 '20 at 01:37
  • 1
    1. Only 80 and 443 ports can be used in the azure web application, so the default of `process.env.PORT` in the code is 80 and 443. So in your code, listening to 3000 and other ports, that is incorrect , Can only listen to one port. – Jason Pan Jul 20 '20 at 01:37
  • 1
    2. Your code does not have a good framework structure. It is recommended to learn koa or egg and then build the framework. The current project configuration or sql statements are all in one file, which is a bit unreasonable. – Jason Pan Jul 20 '20 at 01:37
  • Thanks for the feed back Jason. I see what you are saying about the structure. What changes would I make to allow it to work live? Why does it keep stating 8080 is already used? – williswin Jul 20 '20 at 03:41
  • 1
    https://github.com/764692390/Koa2-Mysql-Layui-ejs – Jason Pan Jul 20 '20 at 05:24
  • I read the structure of this open source code, it should be helpful to you, you can try it. – Jason Pan Jul 20 '20 at 05:26
  • Removing http.createServer(app).listen(app.get('port'), function(){ console.log('Express server listening on port ' + app.get('port')); }); from table_routes allowed the app to work live - seems like it was taking up port 8080, but it is not able to connect to the tables now – williswin Jul 20 '20 at 16:04
  • Will I need to run a second webapp? The sql database is on- Azure Database for MySQL server https://stackoverflow.com/questions/36424405/listen-additional-port-microsoft-azure-nodejs – williswin Jul 20 '20 at 21:22
  • 1
    Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218245/discussion-between-jason-pan-and-williswin). – Jason Pan Jul 21 '20 at 01:27
  • Jason, Is there anyway to expose other ports? Or something like this - https://stackoverflow.com/questions/28755281/nodejs-express-separate-routes-on-two-ports – williswin Jul 22 '20 at 00:31

1 Answers1

0

try without normalizeport function

Jithin Zachariah
  • 313
  • 3
  • 13