javascript - 在javascript中使用node.js的process.env.PORT
问题描述
我在 Node.JS 中有这段代码
var express = require ('express');
var app = express();
var port = process.env.PORT;
app.listen(port, () => console.log(`Listo el puerto ${port}!!`));
在这种情况下,我知道它被分配了一个系统端口。但是在我的 Javascript 中,我尝试使用 Fetch 时有以下编码:
fetch('https://localhost/')
.then(res => res.json())
.then(json => console.log(json));
我的问题是,为了能够在我的 Javascript 文件中使用来自 Node.JS 的答案,我应该在我的端口中放什么?因为我需要一个随机分配我系统的人---->。
https://localhost/port???
谢谢。
解决方案
正如@ringo 所说,对“/path/to/data”之类的 url 的任何获取都将自动使用当前域:端口。
你免费得到这个!
指定域和端口实际上会降低您的应用程序的弹性,因为如果您更改托管应用程序的位置,则需要更新所有内容以解决此问题!
所以从你的例子中:
fetch('/') // we don't include the https protocol, the localhost domain, or the port.
.then(res => res.json())
.then(json => console.log(json));
推荐阅读
- javascript - 如何使用 React.js JavaScript 在 JSX 代码中实现数组递增函数
- angular - 提交函数中的角度错误处理只是挂起
- mysql - 基于计算的 MySQL 用户变量在列有结果时保持为 NULL
- r - 解决 Travis 构建时 rgdal 包失败的问题
- java - 如何使用从 CSV 数据集中读取的 Jmeter 转义逗号?
- build - .Net Core 解决方案在本地构建,但在 Azure DevOPS 管道上构建失败
- ios - 如何在导航栏ios上重叠图像
- facebook - AAD B2C Facebook for Developers IP-Whitelist
- java - 有没有办法在完整的测试运行期间记录 JUnit5 @Disabled 原因值?
- angular - ngModel 值未更新