[英]Why won't Node `https.get` request work on `heroku local` but work when deployed?
I have a Node.js app that I wrote and successfully deployed to a Heroku app via Bash. 我有一个自己编写的Node.js应用程序,并已通过Bash成功部署到了Heroku应用程序。 From my computer running Ubuntu at home it runs fine locally too. 从我在家中运行Ubuntu的计算机上,它在本地也可以正常运行。
However, I have just cloned the repository to a Windows machine and while I've successfully managed to push updates to the Heroku remote, which work, whenever I run heroku local
from Windows and try to access the local version (localhost:5000), that page serves an error and my command line returns TypeError: Request path contains unescaped characters
and layer.js:95.5
. 但是,我刚刚将存储库克隆到Windows机器上,并且当我成功地将更新推送到Heroku远程服务器时,只要我从Windows运行heroku local
并尝试访问本地版本(localhost:5000),该更新就可以正常工作,该页面出现错误,我的命令行返回TypeError: Request path contains unescaped characters
和layer.js:95.5
。
The app uses https.get
and this seems to be the problem line, locally. 该应用程序使用https.get
,这似乎是本地的问题所在。
I'm reluctant to try adapting my code since it works fine on Heroku itself and works fine on my Ubuntu machine -- so can only assume that something needs configuring on my Windows machine. 我不愿意尝试修改我的代码,因为它可以在Heroku上正常工作,也可以在我的Ubuntu计算机上正常工作-因此只能假定需要在Windows计算机上进行配置。
Any idea what the problem might be? 知道可能是什么问题吗?
Be careful which variable keys you choose for config/ .env
in Heroku. 注意在Heroku中为config / .env
选择的变量键。 Windows has some reserved keys (like user
and path
) which will mean trying to set your own values against these keys will not work. Windows有一些保留键(例如user
和path
),这意味着尝试根据这些键设置自己的值将不起作用。
It turns out the issue is with my choice of variable names as used in my .env
file. 事实证明,问题出在我选择的.env
文件中使用的变量名。
When you have data that you don't want to commit to a repository (in my case, an authentication key and details for my account) you can add them to Heroku as "config vars" -- key=value
pairs which Heroku keeps separate to your code so you can version and share your repository as needed and others can add their own details. 当您有不想提交到存储库的数据(在我的情况下,是我的帐户的身份验证密钥和详细信息)时,可以将它们作为“ config vars”添加到Heroku中- key=value
对,Heroku保持分隔到您的代码,以便您可以根据需要对版本库进行版本控制和共享,其他人可以添加自己的详细信息。
When running Heroku locally however using the heroku local
command from the toolbelt, these variables need to come from somewhere else. 但是,在本地使用工具栏上的heroku local
命令运行Heroku时,这些变量需要来自其他地方。 Heroku's help recommends setting them up as key=value
pairs in a file called .env
(which you can then add to .gitignore
to prevent accidental committing. Heroku的帮助建议在名为.env
的文件.env
它们设置为key=value
对(然后可以将其添加到.gitignore
以防止意外提交。
Unfortunately, in my case it was my choice of variable keys in .env
that caused the problem. 不幸的是,就我而言,导致问题的原因是我选择了.env
中的可变键。 I had created dependencies on variables called user
and path
but these seem to be reserved on Windows and have a special purpose and therefore could not be overwritten by what was in my .env
file. 我已经在名为user
和path
变量上创建了依赖项,但这些依赖项似乎在Windows上已保留,并且具有特殊用途,因此无法被.env
文件中的内容覆盖。 This is why what worked for me on my Ubuntu machine would not work directly on Windows. 这就是为什么在我的Ubuntu计算机上对我有用的东西不能在Windows上直接工作的原因。
You can see this in action by calling console.log( process.env.user
, process.env.path
) from Node on a Windows machine. 您可以通过在Windows计算机上从Node调用console.log( process.env.user
, process.env.path
)来查看实际情况。
Now that I have changed the variable names to something non-reserved heroku local
works fine. 现在,我已经将变量名更改为非保留的heroku local
可以正常工作了。 Calling the programme via node
command will still not work as it is not set up to pick up the variables from .env
as Heroku. 通过node
命令调用程序仍然无法进行,因为它没有设置为从.env
以Heroku形式获取变量。
Hope this helps someone else. 希望这对其他人有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.