Node.js + Express - Unable to connect. ERR_CONNECTION_REFUSED

I followed this basic example:

http://shapeshed.com/creating-a-basic-site-with-node-and-express/

Files were created ... they all exist. I ran everything step by step. No matter which browser I use, I get “Unable to connect” (Firefox) and “This webpage is not available ... ERR_CONNECTION_REFUSED” (Chrome) - it just doesn’t work. I checked the generated bin / www file and seemed to indicate port 3000. However, I did not get any output when I ran "node app.js" after creating the site. Looking at this file, I noticed that it pointed to the wrong path for Node on my system, so I changed it to the correct one:

#!/usr/local/bin/ node /** * Module dependencies. */ var app = require('../app'); var debug = require('debug')('rwc:server'); var http = require('http'); /** * Get port from environment and store in Express. */ var port = parseInt(process.env.PORT, 10) || 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); /** * Event listener for HTTP server "error" event. */ function onError(error) { if (error.syscall !== 'listen') { throw error; } // handle specific listen errors with friendly messages switch (error.code) { case 'EACCES': console.error('Port ' + port + ' requires elevated privileges'); process.exit(1); break; case 'EADDRINUSE': console.error('Port ' + port + ' is already in use'); process.exit(1); break; default: throw error; } } /** * Event listener for HTTP server "listening" event. */ function onListening() { debug('Listening on port ' + server.address().port); } 

No dice. Nothing changed. There is no output when running "node app.js" and cannot pull it up. I know that Node exists and is installed correctly, since I already ran a bunch of sample code and played a bit with it.

On OS X Yosemite, but my firewall is turned off.

What's happening? Amazingly little information found when searching on this either - it makes me hesitate to do something serious with Node.

+5
source share
2 answers

Your problem is that the textbook you are following is very old. The express generator has greatly changed its structure over time. Now it uses npm to run the initial application commands, as you would expect. The scripts object in package.json extremely useful for abstracting commands.

Just cd in your sample application and run:

npm start

enter image description here

and enjoy!

The rest of this tutorial, besides setting it up, is still pretty accurate. I would advise docs on anything to be honest. Just my opinion.

Finally . I noticed that he pointed to the wrong path for Node on my system, so I changed it to the correct one. You must change this, or it may fail.

+9
source

Also, if you want the server to work, use nodemon

 nodemon bin/www 

What will give the same o / r:

 [nodemon] 1.11.0 [nodemon] to restart at any time, enter `rs` [nodemon] watching: *.* [nodemon] starting `node ./bin/www bin/wwww` 
0
source

Source: https://habr.com/ru/post/1210866/


All Articles