【发布时间】:2021-10-31 04:11:14
【问题描述】:
我有一个 React Web 应用程序,它当前确实在客户端获取调用以使用实时信息(例如当前天气)更新仪表板,这意味着随着用户的增加,它将导致不必要的流量调用,并且可能可能会导致此天气网站崩溃。
我想了解的是如何使这些 fetch 调用成为服务器端的?我已经研究过创建一个 Node.js Express 服务器,但我不确定它是否具有对远程主机进行 fetch 调用的功能。
不幸的是,这是我的请求天气代码,但它并没有真正起作用。
const { response } = require('express');
const express = require('express');
const app = express();
var fetch = require('node-fetch');
const port = process.env.PORT || 5000;
app.use(express.json());
// This displays message that the server running and listening to specified port
app.listen(port, () => console.log(`Listening on port ${port}`));
// create a GET route
app.get('/request-info', (req, res) => {
res.send({ information: 'information call successful' });
});
app.get('/request-weather', (req, res) => {
fetch('http://thisotherwebsite.com/weather-query-that-returns-json',
{method: 'GET',
headers: {' Accept': 'application/json'}})
.then(res => {
return res;
})
});
【问题讨论】:
标签: node.js reactjs backend react-fullstack