作者:吴小熙1108 | 来源:互联网 | 2023-09-15 15:41
Imadeasimplecreate-react-appapplicationandanode.jsexpressserverside.locallythisworks(
I made a simple create-react-app application and a node.js express server side. locally this works (server serves ui and do api) but when deploying to heroku only UI is served (any API call gets a 503 code)
我创建了一个简单的create-react-app应用程序和一个node.js表达服务器端。本地这个工作(服务器服务ui和做api)但是当部署到heroku时只提供UI(任何API调用得到503代码)
App.js
App.js
// Routes
// works both locally and on heroku
app.get('/ui', (req, res) => {
res.sendFile(path.resolve(__dirname,'build','index.html'));
});
// works only locally
app.use('/users',users);
app.use('/pledge',pledge);
React router
反应路由器
No server side rendering. example for API:
没有服务器端渲染。 API的示例:
app.post('/usersAdd',(req, res) => {
let newUser = new User(req.body);
newUser.save((err) => {
if (err)
return res.send(500, {error: err});
return res.send("successfully saved");
});
});
API call
API调用
export function getUsers() {
return new Promise((resolve, reject) => {
axios.get(serverProps.server + serverProps.getUsers, {
headers: {
'Access-Control-Allow-Origin': '*'
}
})
.then(resolve)
.catch(reject);
});
}
1 个解决方案