我担心我的 react/redux 应用程序的安全性,因为我的 api url 在捆绑的 app.js 文件中公开给公众。我一直在研究这个,一些开发人员以某种方式代理它,即我可以使用 api/
而不是使用我的 api url,每当我使用像 axios 这样的库执行调用时或 superagent它被代理到我的 api url,但这样用户只能看到他们这边的 api/
。
我正在尝试解决这个问题,我假设这是在快速配置中设置的?
最佳答案
您的担忧是合理的。
通常,您会让客户端代码调用/api,并在 express(或您使用的任何服务器)中为“/api”创建一个路由,将请求代理到实际的 api url。
这样您就可以向客户隐藏任何敏感信息。例如身份验证 token 、api key 等。
在 express 中你可以这样做:
app.use('/api', (req, res) => {
const method = req.method.toLowerCase();
const headers = req.headers;
const url = 'your_actual_api_url';
// Proxy request
const proxyRequest = req.pipe(
request({
url
headers,
method,
})
);
const data = [];
proxyRequest.on('data', (chunk) => {
data.push(chunk);
});
proxyRequest.on('end', () => {
const { response } = proxyRequest;
const buf = Buffer.concat(data).toString();
res.status(response.statusCode).send(buf);
});
});
这个例子有点复杂,但它可能适合你。
关于javascript - 在 react/redux 应用程序(代理)中隐藏 api url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37968815/