每服务器 NodeJS 配置
我有第一个 NodeJS 服务器,它与应用程序的其余部分一起部署到客户端服务器。我有一些项目需要在特定于服务器的基础上进行配置,但我无法找到任何简单的方法来处理这个问题。
在谷歌搜索上,我发现了一些选择 - 使用内置支持的框架(太晚了),设置一个可以加载的配置文件(可行,但我不想担心保留一个配置)我们拥有的每台服务器的文件并将其保留在 git 之外)。
我想让节点确定请求域是什么(dev.ourdomain 与 www.ourdomain)并执行一个条件。这听起来很容易,可能也很容易,但我很难找到任何方法来确定该域数据。
I have our first NodeJS server that's being deployed to a client server along with the rest of the application. I have a few items that need to be configured on a server-specific basis but I'm unable to find any easy way to handle this.
On googling I've found a few choices - using a framework that has support built in (too late), setting up a configuration file that can be loaded (would work, but I don't want to have to worry about keep one config file for each server we have and keeping those out of git).
I'd love to just have node determine what the request domain is (dev.ourdomain vs www.ourdomain) and just do a condition. It sounds easy, it likely IS easy, but I'm having trouble finding any way to determine that domain data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 @drachenstern 提到的,您可以使用 request.headers.host,如下所示:
但如果请求是使用 IP 地址而不是服务器名称发出的,则这不会提供规范域。
更好的选择可能是使用服务器的主机名,如下所示:
As @drachenstern mentioned, you could use request.headers.host, as in:
but this wouldn't provide a canonical domain if the request was made using an IP address rather than the server's name.
A better option might be to use the hostname of the server, as in:
您可能会考虑 request.host 是否有您需要的数据。很可能会。
You might consider if request.host has the data you need. It most likely would.
为什么不在 init.js 中硬编码该信息并为每个服务器更改它?您多久需要移动一次服务器来执行此操作?只需执行此
init.js
main.js
我假设您正在 dev.ourdomain 上开发它并将其转储到 www.ourdomain 上。只需忽略转储 init.js 即可保留服务器的 init 版本 ^_^ 这就是我所做的,它使我无需仅仅为了一个命令而使用另一个模块来膨胀项目。
希望这可以帮助遇到这种情况的其他人。
why don't you just hardcode that information in a init.js and change it for each server? How often are you going to move the servers to need to do this? Just do this
init.js
main.js
I assume you are developing it on dev.ourdomain and dumping it on www.ourdomain. Just ignore dumping init.js so that the server's init version remains ^_^ This is what I do and it saves me the need to bloat the project with another module just for one command.
Hope this helps others who encounter this situation.