我首先创建了一个react应用程序,它在客户端进行api调用,这是一个很大的不不,因为它公开了api键。所以我必须创建一个后端express服务器来进行api调用。然而,在客户端,我的react应用程序允许您输入一个随机的用户名(改变状态),以便进行api调用并获取用户信息。是否有方法将状态/道具从客户端转移到后端express以完成api调用?
这是快递代号:
const express = require('express');
const app = express();
const fetch = require('node-fetch');
app.get('/api/customers', (req, res) => {
fetch('https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/huhi?api_key=<MY_API_KEY>')
.then(res => res.json())
.then(result => res.json(result));
});
const port = 5000;
app.listen(port, () => console.log(`Server started on port ${port}`))
在本节中fetch('https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/huhi?api_key=
是否有方法将'by-name/'之后的用户名更改为reginald、doublelift、faker等(存储在客户端react状态中)。
示例
提取('https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/reginald?api_key=
或
提取('https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/doublelift?api_key=
或
提取('https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/faker?api_key=
解决方案之一--
// ...
app.get("/api/customers/:userId", (req, res)=>{
fetch(`https://na1.api.riotgames.com/lol/summoner/v4/summoners/by-name/${req.params.userId}?api_key=<MY_API_KEY>`)
.then(res => res.json())
.then(result => res.json(result));
})
//...
fetch(`/api/customers/${this.state.username}`).then(users => this.setState(users))