Javascript 节点服务在本地工作,但不在heroku上
我制作了一个简单的createreact应用程序和一个node.js express服务器端。 这在本地起作用(服务器提供ui和DoAPI),但在部署到heroku时 仅提供UI(任何API调用都会获得503代码) App.jsJavascript 节点服务在本地工作,但不在heroku上,javascript,node.js,reactjs,express,heroku,Javascript,Node.js,Reactjs,Express,Heroku,我制作了一个简单的createreact应用程序和一个node.js express服务器端。 这在本地起作用(服务器提供ui和DoAPI),但在部署到heroku时 仅提供UI(任何API调用都会获得503代码) App.js // Routes // works both locally and on heroku app.get('/ui', (req, res) => { res.sendFile(path.resolve(__dirname,'build','index.h
// 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);
反应路由器
<ThemeProvider theme={theme}>
<div>
<Router history={history}>
<div>
<Route path={'/ui/'} component={Header}/>
<Route exact={true} path='/ui/welcome' component={Welcome}/>
<Route exact={true} path="/ui/login" component={Login}/>
<Route exact={true} path="/ui/help" component={Help}/>
<Route exact={true} path="/ui/dashboard" component={DashBoard}/>
<Route exact={true} path='/ui/logout' component={Logout}/>
<Route exact={true} path='/ui/pledge' component={Pledge}/>
<Route exact={true} path='/ui/buckets' component={Bucket}/>
<Route exact={true} path='/ui/add' component={AddStuff}/>
<Route exact={true} path='/ui/motion' component={MotionInput}/>
<Route exact={true} path='/ui/status' component={Status}/>
</div>
</Router>
</div>
</ThemeProvider>
API调用
export function getUsers() {
return new Promise((resolve, reject) => {
axios.get(serverProps.server + serverProps.getUsers, {
headers: {
'Access-Control-Allow-Origin': '*'
}
})
.then(resolve)
.catch(reject);
});
}
这是因为在部署时,当要路由
/users
时,它会查找名为users.html的任何文件,但没有找到它,因此返回503。您可以通过将app.get
呼叫替换为以下内容,将所有路由重定向到index.html
来修复此问题:
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, '..', 'build', 'index.html'));
});
看一看。看看它是否有效。这是因为在部署时,在路由
/users
时,它会查找名为users.html的任何文件,但找不到,因此返回503。您可以通过将app.get
呼叫替换为以下内容,将所有路由重定向到index.html
来修复此问题:
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, '..', 'build', 'index.html'));
});
看一看。看看它是否有效。你在用express吗?是的,我在用。需要额外的密码吗?你在用express吗?是的,我在用。需要其他代码吗?谢谢,但是现在服务器端的API调用,如/users不起作用,我得到了一个html页面,我不明白API调用是如何受到影响的。是否使用服务器端渲染?如果可以的话,你能分享代码吗?我看过你的代码,我想知道你是如何在前端使用这些API的。你使用axios吗?是的,axios post和get都使用,我已将此添加到我的问题中。你是否删除了以下行:
app.use('/users',users);应用程序使用(“/质押”,质押)代码>。如果是,那么我不能说是什么导致了给定代码的问题。也许您在使用axios时遇到了一些问题。谢谢,但是现在服务器端API调用如/users不起作用,我收到了一个html页面。我不明白API调用是如何受到影响的。是否使用服务器端渲染?如果可以的话,你能分享代码吗?我看过你的代码,我想知道你是如何在前端使用这些API的。你使用axios吗?是的,axios post和get都使用,我已将此添加到我的问题中。你是否删除了以下行:app.use('/users',users);应用程序使用(“/质押”,质押)代码>。如果是,那么我不能说是什么导致了给定代码的问题。也许您在如何使用axios方面存在一些问题。