I am working through "Mean Machine: A beginner’s practical guide to the JavaScript stack" by Chris Sevilleja. My problem is on page 60. Per the book's instructions, I have created a small server.js
file, along with the package.json
, and used npm install
to create the node_modules folder
and populate it with the appropriate modules. When I run 'server.js'.
I received a message in the terminal running the console.log()
message which I placed at the end of the program and hence I know the program executed.
But when I then go to Chrome and put 'https://localhost:8080
" in the search bar, I receive the following error message:
This site can’t be reached
localhost unexpectedly closed the connection.
ERR_CONNECTION_CLOSED
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:
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME node 6188 23u IPv6 0x3a96cd0448d7ba87 0t0 TCP *:http-alt (LISTEN)
(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.
My code is below:
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);
Any suggestions to get my 'res.send('Welcome to the home page!');' message to display would be fantastic.
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.
Sort of a follow up: I'm using SublimeText. Is this problem something I could identify/solve using a more sophisticated IDE?
instead of "https://localhost:8080" put "http://localhost:8080" in the chrome. remove s.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With