英文:
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
显示帐户应用程序。
当我访问 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'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't forget to set up subdomains in heroku and google domains, and don't forget to add env variable in heroku
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论