[英]Pattern for a React component with access to lowdb
我正在寻找反应组件的模式。 它应该包括lowdb。 有人已经建好了?
好的,我不通过前端访问 lowdb。 我使用 Axios 向我的 node/express 后端发送请求并使用 lowdb-data 进行响应。 要求您知道如何与不同端口上的 react 前端和 node/express 后端进行交互。 I dont let express render my react-app... looks something like this and axios as a dependency in the package.json + import Axios from 'axios':
export default class Status extends Component {
constructor(props) {
super(props);
this.state = "";
}
componentWillMount() {
Axios.get("http://localhost:4000/dbUserAuth").then((res) => {
let authState = res.data;
if (authState) {
this.setState({ authState });
}
});
}
这会向我的 node.js/express 后端发送一个请求,该后端在端口 4000 上运行。前端在 2000 上运行。后端看起来像这样:
server.get("/dbUserAuth", (request, response) => {
function resAuthData() {
let array = [];
const dbUserAuth = db.get("UserAuth[0]").value();
const dbChatIdFound = db.get("UserAuth[1]").value();
const botActive = db.get("UserAuth[2]").value();
array.push(dbUserAuth, dbChatIdFound, botActive);
response.send(array);
}
resAuthData();
});
希望有人需要这些片段。 此致
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.