[英]Using subdomains locally on an express app
我有在 express 上運行的節點應用程序。 我想設置兩種不同的帳戶類型(一種用於買家,一種用於賣家)並讓它們可以通過不同的子域(即 buyers.example.com 和 sellers.example.com)訪問。 我不確定如何在本地進行設置。 我知道我可以在我的機器上編輯主機文件以將*.localhost
解析為127.0.0.1
,但這並不能解決我的問題。 在這種情況下, buyers.localhost/login
和 sellers.localhost/ sellers.localhost/login
將路由到同一個地方。 這是一個常見問題嗎?如果是,處理此問題的標准方法是什么? 我有哪些選項可以分離處理兩種帳戶類型的邏輯?
首先添加:
app.get('*', function(req, res, next){
if (req.headers.host == 'buyers.localhost:5000') { //Port is important if the url has it
req.url = '/buyers' + req.url;
}
else if(req.headers.host == 'sellers.localhost:5000') {
req.url = '/sellers' + req.url;
}
next();
});
接着:
app.get('/login', function(){
//Default case, no subdomain
})
app.get('/buyers/login', function(){
//Buyers subdomain
})
app.get('/sellers/login', function(){
//Sellers subdomain
})
在這里了解到這一點: https : //groups.google.com/forum/#!topic/ express-js/ sEu66n8Oju0
app.all("*", function (req: Request, res: Response, next: NextFunction) { if (req.headers.host == `v1.${process.env.SERVER_DOMAIN}`) req.url = `/v1${req.url}`; // ? <= direct from v1 to domain/v1 if (req.headers.host == `api.${process.env.SERVER_DOMAIN}`) req.url = `/api${req.url}`; // ? <= direct from v1 to domain/api next(); }); //Port is important if the url has it
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.