将HTTP请求从客户端发送到Heroku平台内的服务器



我的应用程序在客户端和nodejs中使用angular作为后端,但是在heroku的生产中正确配置它们时遇到了一些问题。

在开发中,我在端口 3000 上运行我的 nodejs 服务器,并将所有 http 请求从 angular 客户端发送到 http://localhost:3000,并且运行良好。但是当我在dist目录下部署我的NodeJS服务器和我的Angular客户端文件时,heroku在随机端口上提升服务器,我所有对http://localhost:3000 http请求都失败了。如何确定 Heroku 上的节点端口,或者如何在我的 Angular 客户端中获取它?

服务器上的代码:

let port = normalizePort(process.env.PORT || '3000');
app.set('port', port);
const server = http.createServer(app);
server.listen(port, () => console.log('app listening on port ' 
+ port));
server.on('error', onError);
server.on('listening', onListening);

客户端上的代码:

export const environment = {
  production: true,
  serverUrl: 'http://localhost:3000'
};

我认为您可以直接使用Heroku提供的后端的URL。此处不需要任何端口。因为它将使用端口 80 作为默认端口。 Eg: https://example.herokuapp.com/

最新更新