我开发了一个 MERN 项目。在开发过程中,客户端监听3000端口,服务器监听5000端口。现在项目托管在heroku上。项目部署成功。当我加载站点时,它侦听服务器端口 5000。 setupProxy.js 是
const createProxyMiddleware = require('http-proxy-middleware');
module.exports = function (app) {
app.use('/api', createProxyMiddleware({
target: 'http://localhost:5000',
changeOrigin: true,
})
);
};
server.js 是
app.get('/', (req, res) => {
res.send('Server is Ready');
});
if (process.env.NODE_ENV === 'production') {
app.use(express.static('../frontend/build'));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, '../frontend', 'build', 'index.html'));
});
}
app.use((err, req, res, next) => {
res.status(500).send({ message: err.message });
});
const port = process.env.PORT || 5000;
app.listen(port, () => {
console.log(`Serve at http://localhost:${port}`);
});
客户端和服务器像兄弟一样位于不同的文件夹中。
如何在加载 Web 应用 url 时移植到客户端。应用程序网址是 App Link