How do I resolve this “localhost unexpectedly closed the connection” error?
<p>I am working through "<strong>Mean Machine: A beginner’s practical guide to the JavaScript stack</strong>" by Chris Sevilleja. My problem is on page 60. Per the book's instructions, I have created a small <code>server.js</code> file, along with the <code>package.json</code>, and used <code>npm install</code> to create the <code>node_modules folder</code> and populate it with the appropriate modules. When I run 'server.js'. 
I received a message in the terminal running the <code>console.log()</code> message which I placed at the end of the program and hence I know the program executed. </p>

<p>But when I then go to Chrome and put '<code>https://localhost:8080</code>" in the search bar, I receive the following error message:</p>

<blockquote>
  <p>This site can’t be reached</p>
  
  <p>localhost unexpectedly closed the connection.</p>
  
  <p>ERR_CONNECTION_CLOSED</p>
</blockquote>

<p>I have spent over an hour trying to figure this out and am pretty lost. If I use 'http' instead of 'https', it just takes forever to load (I waited for about 5 minutes). I have checked whether I have asynchronous DNS flag in Chrome or something and confirmed that it is enabled. I tried doing "$ lsof -i:8080" on the terminal and got:</p>

<blockquote>
  <p>COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  node 6188 23u IPv6 0x3a96cd0448d7ba87 0t0 TCP *:http-alt (LISTEN)</p>
</blockquote>

<p>(Not really sure what it does but I saw it on a webpage addressing a similar problem and the responders instructed the person to do that.) I have tried accessing it on Safari as well and receive the same message.</p>

<p>My code is below:</p>

<pre><code>var express = require('express');
var bodyParser = require('body-parser');
var morgan = require('morgan');
var mongoose = require('mongoose');

var port = process.env.PORT || 8080; // set the port for our app

var app = express(); // define our app in terms of Express
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json);

// configure out app to handle CORS requests
app.use(function(req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', '*');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, content-type, Authorization');
    next();
});

// log all requests to the console
app.use(morgan('dev'));

// API routes

// basic route for the home page
// route, not middleware, so no 'next' parameter
app.get('/', function(req, res) {
    res.send('Welcome to the home page!');
});

// get an instance of the express router
var apiRouter = express.Router();

// test route to make sure everything is working
// accessed at GET http://localhost:8080/api
apiRouter.get('/', function(req, res) {
    res.json({message: 'Welcome to our API.'});
});

// mount apiRouter on our app
// they will all be prefixed with /api
app.use('/api', apiRouter);

// start the server on the port we indicated on line 6
app.listen(port);
console.log('Magic happens on port ' + port);
</code></pre>

<p>Any suggestions to get my 'res.send('Welcome to the home page!');' message to display would be fantastic. </p>

<p>Thanks in advance. I've tried to describe the problem as well as I can but if there is any other information you need please let me know.</p>

<p>Sort of a follow up: I'm using SublimeText. Is this problem something I could identify/solve using a more sophisticated IDE?</p>