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

时间:2019-03-30 10:11:34

标签: node.js angular web heroku

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

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

服务器上的代码

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'
};

1 个答案:

答案 0 :(得分:1)

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