子域名与实际域名不兼容。

huangapple go评论72阅读模式
英文:

Subdomain Doesn't Work With Actual Domain

问题

我有这段代码:

const app = express();

const accountApp = express();
const publicApp = express();

publicApp.use(express.static(path.join(process.cwd(), "web/dist/public")));
accountApp.use(express.static(path.join(process.cwd(), "web/dist/account")));

app.use(vhost('account.*', accountApp));
app.use(publicApp);

它在本地主机上运行得很完美:http://localhost:3000 显示公共应用程序,http://account.localhost:3000 显示帐户应用程序。

我将其托管在Heroku上,并添加了子域名,如下所示:
子域名与实际域名不兼容。

并且在Google Domains上配置如下:
子域名与实际域名不兼容。

当我访问 www.stravamenu.com 时,它显示了公共应用程序,正如应该的那样。但是在 account.stravamenu.com 上,它显示了公共应用程序,而不是帐户应用程序。

问题在于 vhost 吗?还是因为 Heroku 或 Google Domains?

英文:

I have this code:

const app = express();

const accountApp = express();
const publicApp = express();

publicApp.use(express.static(path.join(process.cwd(), "web/dist/public")));
accountApp.use(express.static(path.join(process.cwd(), "web/dist/account")));

app.use(vhost('account.*', accountApp));
app.use(publicApp);

It works perfectly on localhost: http://localhost:3000 shows public app, http://account.localhost:3000 shows account app

I hosted it on heroku, added subdomain like this:
子域名与实际域名不兼容。

And this is on google domains:
子域名与实际域名不兼容。

And when I go to www.stravamenu.com it show public app, as it should. But on account.stravamenu.com it shows public app instead of account app.

Is the problem in vhost? Or is it heroku or google domains?

答案1

得分: 0

app.use(vhost('account.*.*', accountApp));
app.use(vhost("www.*.*", publicApp));

由于某种原因,'account.' 与 'account..' 不相同。所以 'account.' 将在本地主机上运行。如果您想将 'account..' 更改为 'account.*',我建议添加一个检查是否处于开发模式的语句:

const isProd = proccess.env.PROD;

if(isProd) {
    app.use(vhost('account.*.*', accountApp));
    app.use(vhost("www.*.*", publicApp));
} else {
   app.use(vhost('account.*', accountApp));
   app.use(publicApp); // 如果您不想在本地主机之前添加 www
}

不要忘记在Heroku和Google Domains中设置子域名,并不要忘记在Heroku中添加环境变量。


<details>
<summary>英文:</summary>

**Solution:**

app.use(vhost('account..', accountApp));
app.use(vhost("www..", publicApp));


For some reason `account.*` is not the same as `account.*.*`. So `account.*` will work on localhost. If you want to change `account.*.*` with `account.*` I recommend adding a statement that checks if you&#39;re in dev mode:

const isProd = proccess.env.PROD;

if(isProd) {
app.use(vhost('account..', accountApp));
app.use(vhost("www..", publicApp));
} else {
app.use(vhost('account.*', accountApp));
app.use(publicApp); // if you don't want to add www before localhost
}


Don&#39;t forget to set up subdomains in heroku and google domains, and don&#39;t forget to add env variable in heroku

</details>



huangapple
  • 本文由 发表于 2023年1月4日 10:27:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75000407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定