每服务器 NodeJS 配置

发布于 2024-11-07 23:46:39 字数 286 浏览 0 评论 0原文

我有第一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小草泠泠 2024-11-14 23:46:39

正如 @drachenstern 提到的,您可以使用 request.headers.host,如下所示:

# get the path portion of the URI without optional port
var domain=request.headers.host.replace(/\:\d+$/,''); 

但如果请求是使用 IP 地址而不是服务器名称发出的,则这不会提供规范域。

更好的选择可能是使用服务器的主机名,如下所示:

var domain=process.env[
  process.env['OS'] && process.env['OS'].match(/^Win/) ? 'HOSTNAME' : 'HOST'
];

As @drachenstern mentioned, you could use request.headers.host, as in:

# get the path portion of the URI without optional port
var domain=request.headers.host.replace(/\:\d+$/,''); 

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:

var domain=process.env[
  process.env['OS'] && process.env['OS'].match(/^Win/) ? 'HOSTNAME' : 'HOST'
];
拥醉 2024-11-14 23:46:39

您可能会考虑 request.host 是否有您需要的数据。很可能会。

You might consider if request.host has the data you need. It most likely would.

逆蝶 2024-11-14 23:46:39

为什么不在 init.js 中硬编码该信息并为每个服务器更改它?您多久需要移动一次服务器来执行此操作?只需执行此

init.js

module.exports = { domain: "dev.ourdomain"};

main.js

var domain = require( "init.js" ).domain;

我假设您正在 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

module.exports = { domain: "dev.ourdomain"};

main.js

var domain = require( "init.js" ).domain;

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文