【发布时间】:2020-08-13 23:11:24
【问题描述】:
我正在尝试将 API 调用链接到我的后端以测试我的服务器是否正在运行。我在前端使用 React,在后端使用带有 express 的 Node.js。
我的后端端点很简单:
app.get('/', (req, res) => {
console.log('hit')
const name = req.query.name || 'World';
res.setHeader('Content-Type', 'application/json');
res.send({
greeting: `Hello ${name}!`,
});
});
我在前端提出请求:
React.useEffect(() => {
fetch('http://localhost:3001//')
.then((res) => res.json())
.then((data) => {
console.log(data);
})
.catch(console.log('error'));
});
我添加了控制台日志以尝试追踪错误。我将“错误”记录到控制台。 此外,我收到控制台错误:
Access to fetch at 'http://localhost:3001/' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
manifest.json:1 GET http://localhost:3000/manifest.json 404 (Not Found)
manifest.json:1 Manifest: Line: 1, column: 1, Syntax error.
我之前没见过这个 manifest.json 警告。
【问题讨论】:
-
@IanKemp 会修复 manifest.json 吗?
-
请使用 cors 与 express expressjs.com/en/resources/middleware/cors.html
-
忽略
manifest.json error,这只是Chrome在做Chrome的事情。
标签: javascript node.js reactjs express