【发布时间】:2021-11-12 05:20:20
【问题描述】:
我正在尝试从我的客户端 vanilla JS 向我的 ExpressJS 服务器运行一个简单的 GET 请求 - 我的客户端在端口 80 上的 nginx 静态服务器上运行,而我的 Express 服务器在端口 3000 上的 nodejs 上运行。
我不断收到 chrome 错误消息:
GET http://localhost:3000/ net::ERR_CONNECTION_REFUSED
客户端 JS:
async function ping() {
const response = await fetch('http://localhost:3000');
return response;
}
服务器端 JS:
const express = require('express');
const cors = require('cors');
const app = express();
const port = 3000;
app.use(express.json);
app.use(cors());
app.get('/', (req, res) => {
res.send('Hello world!');
});
app.listen(port, () => {
console.log(`Server running on port ${port}`);
});
如果有人可以帮助我,将不胜感激,在此先感谢您!
【问题讨论】:
-
欢迎来到 Stackoverflow!
app.use(express.json)应该是app.use(express.json()),但我不知道这是否已经解决了你的问题。 -
我已经复制了你的代码,在谷歌浏览器上
http://localhost:3000我得到了你好世界 -
连接被拒绝表明 localhost:3000 上实际上没有服务器运行
标签: node.js express fetch-api connection-refused