【问题标题】:sending http requests from client to server inside heroku platform在heroku平台内从客户端向服务器发送http请求
【发布时间】:2019-08-21 03:48:15
【问题描述】:

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

在开发过程中,我在端口 3000 上运行我的 nodejs 服务器,并将来自 angular 客户端的所有 http 请求发送到 http://localhost:3000,它运行良好。 但是当我在 dist 目录下使用我的 Angular 客户端文件部署我的 nodejs 服务器时,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'
};

【问题讨论】:

    标签: node.js angular web heroku


    【解决方案1】:

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

    【讨论】:

    • 是的,有可能,但我认为向 localhost + 端口发送请求会比向域名发送 http 请求快得多。也许我错了,你的方式被认为是最好的做法
    • 据我所知,在 Heroku 中,无法知道应用程序部署在哪个 IP 和端口上。使用域名总是更好。云提供商足够聪明,可以快速解析 DNS。
    猜你喜欢
    • 2020-07-15
    • 2021-11-09
    • 1970-01-01
    • 1970-01-01
    • 2015-06-15
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多