This is my Node.js
code:
// Using ES6 syntax is fine because we already have Babel to transpile code.
import express from 'express';
// Create an Express application.
const app = express();
// Define port number for the server.
const PORT = 400;
// When visitors make a GET request to address "/", specify the response.
app.get('/', (req, res) =>
res.send(`Node and Express server running on port ${PORT}`)
);
// Send message to the console to confirm that the server is running on the specified port.
app.listen(PORT, () =>
console.log(`Server running on port ${PORT}`)
);
When I use $ npm start
, I get this:
> smr@1.0.0 start /Users/jaimemontoya/Desktop/SMR
> nodemon ./index.js --exec babel-node -e js
[nodemon] 2.0.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching dir(s): *.*
[nodemon] watching extensions: js
[nodemon] starting `babel-node ./index.js`
Server running on port 400
However, when I visit http://localhost:4000/ from a web browser, I see this:
This site can’t be reached
localhost refused to connect.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED
Any ideas to make this work from a web browser?
UPDATE 1:
I meant http://localhost:400
I guess PORT 400 was not available for me. I used const PORT = 406;
and now when I visit http://localhost:406/, it works, I get this message:
Node and Express server running on port 406