节点和Express服务器在终端上运行,但Web浏览器显示“ ERR_CONNECTION_REFUSED localhost拒绝连接。”

问题描述 投票:0回答:1

这是我的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时,我得到了:

> [email protected] 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

但是,当我从Web浏览器访问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

enter image description here

有什么想法可以通过网络浏览器来实现?

UPDATE 1:

我的意思是http://localhost:400

node.js express localhost nodemon babel-node
1个回答
0
投票

我想PORT 400对我不可用。我使用了const PORT = 406;,现在访问http://localhost:406/时,它会显示以下消息:

在端口406上运行的节点和Express服务器

© www.soinside.com 2019 - 2024. All rights reserved.