title: Unable to connect error - Node Express app on Linux server
body: <p>I receive an 'Unable to Connect' error in my browser when trying to connect to my Node Express application. At (my servers ip address) 1.1.1.1:5000. The application works fine in my development environment but not on my AWS EC2 Linux server.</p>
  
  <ul>
  <li>The Node Express app works on my computer in dev</li>
  <li>Port 5000 is allowing incoming TCP. I tested and confirmed this with a smaller application (<a href="https://hackernoon.com/deploying-a-node-app-on-amazon-ec2-d2fb9a6757eb" rel="nofollow noreferrer">https://hackernoon.com/deploying-a-node-app-on-amazon-ec2-d2fb9a6757eb</a>).</li>
  <li>I confirmed my Node Express application is running . (I am using pm2)</li>
  <li>PM2 keeps restarting my Node Express app at ~14s</li>
  <li>I tried to curl from my machine to hit port 5000, I received a connection refused error <code>curl: (7) Failed to connect to 1.1.1.1 port 5000: Connection refused</code></li>
  </ul>
  
  <p><strong>UPDATE</strong></p>
  
  <ul>
  <li>Instead of starting the application with <code>pm2 start app.js</code> I started it with <code>npm start</code> and I the app is hosted at port 5000 successfully. </li>
  <li>I can go to 1.1.1.1:5000 and am returned <code>API is running</code></li>
  <li>I use js <code>fetch</code> api to call the backend at 127.0.0.1:5000 and receive a <code>Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://127.0.0.1:5000/pickTicket/21780482. (Reason: CORS request did not succeed).
  2
  TypeError: NetworkError when attempting to fetch resource.</code> (*Note: My api is on the same server as my nginx/react app)`</li>
  </ul>
  
  <p>My application starts with app.js</p>
  
  <pre><code>var createError = require('http-errors');
  var express = require('express');
  var path = require('path');
  var cookieParser = require('cookie-parser');
  var logger = require('morgan');
  var cors = require('cors');
  var compression = require('compression');
  var fetch = require('node-fetch');
  
  var pickTicketRouter = require('./routes/pickTicket');
  var kdlRouter = require('./routes/kdl')
  
  console.log('Creating API')
  var app = express();
  app.use(cors());
  app.options('*', cors());
  
  // view engine setup
  app.set('views', path.join(__dirname, 'views'));
  app.set('view engine', 'jade');
  
  app.use(compression());
  app.use(logger('dev'));
  app.use(express.json());
  app.use(express.urlencoded({ extended: false }));
  app.use(cookieParser());
  app.use(express.static(path.join(__dirname, 'public')));
  
  app.get('/', (req, res) =&gt; {
    res.statusCode = 200;
    res.setHeader('Content-Type', 'text/plain');
    res.end('API is running\n');
  });
  
  app.use('/pickTicket', pickTicketRouter);
  app.use('/kdl', kdlRouter)
  
  // catch 404 and forward to error handler
  app.use(function(req, res, next) {
    next(createError(404));
  });
  
  // error handler
  app.use(function(err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};
  
    // render the error page
    res.status(err.status || 500);
    res.render('error');
  });
  
  module.exports = app;
  </code></pre>
  
  <p>/bin/www</p>
  
  <pre><code>#!/usr/bin/env node
  
  /**
   * Module dependencies.
   */
  
  var app = require('../app');
  var debug = require('debug')('api:server');
  var http = require('http');
  
  /**
   * Get port from environment and store in Express.
   */
  
  var port = normalizePort(process.env.PORT || '5000');
  console.log('Listening on port: ', port);
  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 &gt;= 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);
  }
  
  </code></pre>
  
  <p>package.json</p>
  
  <pre><code>{
    "name": "api",
    "version": "0.0.0",
    "private": true,
    "scripts": {
      "start": "node ./bin/www",
      "dev": "nodemon ./bin/www"
    },
    "dependencies": {
      "compression": "^1.7.4",
      "cookie-parser": "~1.4.4",
      "cors": "^2.8.5",
      "debug": "~2.6.9",
      "express": "~4.16.1",
      "forever": "^1.0.0",
      "http-errors": "~1.6.3",
      "jade": "~1.11.0",
      "morgan": "~1.9.1",
      "mssql": "^5.1.0",
      "node-fetch": "^2.6.0",
      "sequelize": "^5.11.0",
      "tedious": "^6.2.0"
    },
    "devDependencies": {
      "nodemon": "^1.19.1"
    }
  }
  </code></pre>
  
  <p>I expect to see get a response from the api but instead got a CORS error.</p>