终端上的节点和 Express 服务器 运行,但网络浏览器显示 "ERR_CONNECTION_REFUSED localhost refused to connect."
Node and Express server running on the terminal but web browser says "ERR_CONNECTION_REFUSED localhost refused to connect."
这是我的 Node.js
代码:
// 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}`)
);
当我使用 $ npm start
时,我得到这个:
> 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
但是,当我从网络浏览器访问 http://localhost:4000/ 时,我看到了:
This site can’t be reached
localhost refused to connect.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED
有什么想法可以使它在网络浏览器中运行吗?
更新 1:
我的意思是http://localhost:400
我想 PORT 400 对我来说不可用。我使用 const PORT = 406;
,现在当我访问 http://localhost:406/ 时,它起作用了,我收到这条消息:
Node and Express server running on port 406
这是我的 Node.js
代码:
// 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}`)
);
当我使用 $ npm start
时,我得到这个:
> 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
但是,当我从网络浏览器访问 http://localhost:4000/ 时,我看到了:
This site can’t be reached
localhost refused to connect.
Try:
Checking the connection
Checking the proxy and the firewall
ERR_CONNECTION_REFUSED
有什么想法可以使它在网络浏览器中运行吗?
更新 1:
我的意思是http://localhost:400
我想 PORT 400 对我来说不可用。我使用 const PORT = 406;
,现在当我访问 http://localhost:406/ 时,它起作用了,我收到这条消息:
Node and Express server running on port 406