gpt4 book ai didi

node.js - 带有 Node 服务器和react-admin的API URL

转载 作者:太空宇宙 更新时间:2023-11-03 21:50:22 25 4
gpt4 key购买 nike

我正在尝试连接 react 管理员:https://github.com/marmelab/react-admin和 Node 服务器:https://github.com/hagopj13/node-express-mongoose-boilerplate

我在数据库中有用户,我可以使用 react 管理员登录。但是当我想显示用户列表时,我收到此错误:

fetch.js:39 GET http://localhost:4000/v1/users?filter=%7B%7D&range=%5B0%2C24%5D&sort=%5B%22createdAt%22%2C%22DESC%22%5D 401 (Unauthorized)

我不知道要在服务器上修改什么来接受请求。

我有点迷失了,我需要帮助才能开始,你有与react-admin一起使用的示例吗?我需要修改哪些文件?

更新:我在react-admin中的数据提供者

import { fetchUtils } from "react-admin";
import { stringify } from "query-string";

const apiUrl = "http://localhost:4000/v1";
const httpClient = fetchUtils.fetchJson;

export default {
getList: (resource, params) => {
console.log(params.pagination);
console.log(params.sort);
const { page, perPage } = params.pagination;
const { field, order } = params.sort;
const query = {
sort: JSON.stringify([field, order]),
range: JSON.stringify([(page - 1) * perPage, page * perPage - 1]),
filter: JSON.stringify(params.filter)
};
const url = `${apiUrl}/${resource}?${stringify(query)}`;

return httpClient(url).then(({ headers, json }) => ({
data: json,
total: parseInt(
headers
.get("content-range")
.split("/")
.pop(),
10
)
}));
},

getOne: (resource, params) =>
httpClient(`${apiUrl}/${resource}/${params.id}`).then(({ json }) => ({
data: json
})),

getMany: (resource, params) => {
const query = {
filter: JSON.stringify({ id: params.ids })
};
const url = `${apiUrl}/${resource}?${stringify(query)}`;
return httpClient(url).then(({ json }) => ({ data: json }));
},

getManyReference: (resource, params) => {
const { page, perPage } = params.pagination;
const { field, order } = params.sort;
const query = {
sort: JSON.stringify([field, order]),
range: JSON.stringify([(page - 1) * perPage, page * perPage - 1]),
filter: JSON.stringify({
...params.filter,
[params.target]: params.id
})
};
const url = `${apiUrl}/${resource}?${stringify(query)}`;

return httpClient(url).then(({ headers, json }) => ({
data: json,
total: parseInt(
headers
.get("content-range")
.split("/")
.pop(),
10
)
}));
},

update: (resource, params) =>
httpClient(`${apiUrl}/${resource}/${params.id}`, {
method: "PUT",
body: JSON.stringify(params.data)
}).then(({ json }) => ({ data: json })),

updateMany: (resource, params) => {
const query = {
filter: JSON.stringify({ id: params.ids })
};
return httpClient(`${apiUrl}/${resource}?${stringify(query)}`, {
method: "PUT",
body: JSON.stringify(params.data)
}).then(({ json }) => ({ data: json }));
},

create: (resource, params) =>
httpClient(`${apiUrl}/${resource}`, {
method: "POST",
body: JSON.stringify(params.data)
}).then(({ json }) => ({
data: { ...params.data, id: json.id }
})),

delete: (resource, params) =>
httpClient(`${apiUrl}/${resource}/${params.id}`, {
method: "DELETE"
}).then(({ json }) => ({ data: json })),

deleteMany: (resource, params) => {
const query = {
filter: JSON.stringify({ id: params.ids })
};
return httpClient(`${apiUrl}/${resource}?${stringify(query)}`, {
method: "DELETE",
body: JSON.stringify(params.data)
}).then(({ json }) => ({ data: json }));
}
};

感谢和问候

最佳答案

您在请求 header 中缺少 JWT token ,这就是为什么您收到 401 代码,路由/v1/users 使用 auth 中间件保护 .get(auth('getUsers '), validate(userValidation.getUser), userController.getUser) 在 paths/v1/user.route.js

在 middlewares/auth.js 中 passport.authenticate('jwt', { session: false }, verifyCallback(req, resolve,拒绝, requiredRights))(req, res, next);

关于node.js - 带有 Node 服务器和react-admin的API URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60182711/

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