gpt4 book ai didi

node.js - 无法从本地主机获取数据

转载 作者:行者123 更新时间:2023-12-04 16:41:26 27 4
gpt4 key购买 nike

我用 NodeJs 制作了我的第一个 API

如果我尝试从浏览器的 url 访问资源,这就是我得到的结果

enter image description here

我成功访问了 postman 的帖子。现在我试着用一个图形站点设置一个小调用,但我无法获取数据

这是我在传奇中尝试过的调用

export const fetchWrapper = async url => {
var request = new Request({
url: url,
method: "GET"
});
await fetch(request)
.then(res => res.json())
.then(
result => {
return result;
},
error => {
return error;
}
);
};

这就是传奇

export function* getPosts() {
const url = `http://localhost:8080/feed/posts`;
try {
const data = yield call(fetch(url), {method: 'GET'});
console.log('data',)
yield put(getPostsResponse({ data }));
} catch (e) {
console.log("error", e);
}
}

这些是我在控制台中遇到的错误

enter image description here

enter image description here

更新

正如评论中所建议的,这是我的 Node 代码

Controller /feed.js

exports.getPosts = (req, res, next) => {
res.json({
posts: [
{ id: 1, title: "Titolo 1", description: "descrizione 1" },
{ id: 2, title: "Titolo 2", description: "descrizione 2" },
{ id: 3, title: "Titolo 3", description: "descrizione 3" }
]
});
};

exports.createPosts = (req, res, next) => {
const title = req.body.title;
const description = req.body.description;

const ID = 1234;

res.status(201).json({
message: "success operation",
post: {
id: ID,
title: title,
description: description
}
});
};

路由/feed.js

const express = require("express");
const router = express.Router();

const feedController = require("../controllers/feed");

router.get("/post", feedController.getPosts);
router.post("/post", feedController.createPosts);


module.exports = router;

应用程序.js

const express = require('express');
const bodyParser = require("body-parser");
const feedRoute = require('./route/feed');

const app = express();

app.use(bodyParser.json()); //application json
app.use('/feed', feedRoute);
app.listen(8080);

更新

useEffect(() => {
// getPosts();
fetch("http://localhost:8080/feed/post")
.then(resp => resp.json())
.then(data => console.log('data', data));
}, [getPosts]);

也尝试过这个,但没有,我收到同样的错误。

预期行为:

我必须成功调用本地主机服务器。

解决方案

正如 ivani 建议的那样,我刚刚启用了 CORS,这是我添加到 app.js 的代码。不是最好的解决方案,但现在我可以看到响应。

const allowedOrigins = ["http://localhost:3000", "http://localhost:8080"];

app.use(
cors({
origin: function(origin, callback) {
if (!origin) return callback(null, true);
if (allowedOrigins.indexOf(origin) === -1) {
var msg =
"The CORS policy for this site does not " +
"allow access from the specified Origin.";
return callback(new Error(msg), false);
}
return callback(null, true);
}
})
);

最佳答案

正如 ivani 所建议的,我刚刚启用了 CORS。

我在nodeJs的App.js中添加了这个

const allowedOrigins = ["http://localhost:3000","http://localhost:8080"];

app.use(
cors({
origin: function(origin, callback) {
if (!origin) return callback(null, true);
if (allowedOrigins.indexOf(origin) === -1) {
var msg =
"The CORS policy for this site does not " +
"allow access from the specified Origin.";
return callback(new Error(msg), false);
}
return callback(null, true);
}
})
);

现在我可以访问数据了

enter image description here

这是整个 App.js 文件

const express = require('express');
const bodyParser = require("body-parser");
const feedRoute = require('./route/feed');
const cors = require("cors");

const app = express();

const allowedOrigins = ["http://localhost:3000", "http://localhost:8080"];

app.use(
cors({
origin: function(origin, callback) {
if (!origin) return callback(null, true);
if (allowedOrigins.indexOf(origin) === -1) {
var msg =
"The CORS policy for this site does not " +
"allow access from the specified Origin.";
return callback(new Error(msg), false);
}
return callback(null, true);
}
})
);

app.use(bodyParser.json()); //application json
app.use('/feed', feedRoute);
app.listen(8080);

关于node.js - 无法从本地主机获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60084428/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com