简体   繁体   English

域之前的URL参数

[英]URL parameters before the domain

I have a question about routing and urls in general. 我对一般的路由和网址有疑问。 My question regards parameters or queries in the url before the domain itself. 我的问题是关于域本身之前的url中的参数或查询。 For example: 例如:

http://param.example.com/ http://param.example.com/

I ask this question because I am using ExpressJS and you only define your routes after the domain itself like: 我问这个问题是因为我使用的是ExpressJS,而您只能在域本身之后定义路由,例如:

http://example.com/yourRoute http://example.com/yourRoute

Which would be added to the end of the url. 它将被添加到URL的末尾。 What I want is to be able to store parameters inbefore the domain itself. 我想要的是能够在域本身之前存储参数。

Thank you in advance! 先感谢您!

FYI 费耶

I do know how to use parameters and queries, I just don't know how I would go about to insert them before the domain. 我确实知道如何使用参数和查询,只是不知道如何在域之前插入它们。

Maybe this vhost middleware is useful for your situation: https://github.com/expressjs/vhost#using-with-connect-for-user-subdomains 也许此虚拟主机中间件对您的情况很有用: https : //github.com/expressjs/vhost#using-with-connect-for-user-subdomains

Otherwise a similar approach would work: create a middleware function that parses the url and stores the extracted value in an attribute of the request. 否则,类似的方法将起作用:创建一个中间件函数,该函数解析url并将提取的值存储在请求的属性中。

You can create an if statement which can look at the sub-domain through the express req.headers.host variable which contains the domain of the request. 您可以创建一个if语句,该语句可以通过包含请求域的express req.headers.host变量查看子域。 For example: 例如:

-- google.com/anything/another
req.headers.host => "google.com"

-- docs.google.com/anything/
req.headers.host => "docs.google.com"

So working off this in your route you can call Next() if the request doesn't match the form you want. 所以工作过这个在你的路线,你可以拨打Next()如果请求不符合你想要的形式。

router.get('/', function(req, res, next) {
        if (req.headers.host == "sub.google.com") {
            //Code for res goes here
        } else {
            //Moves on to next route option b/c it didn't match
            next();
        }
    });

This can be expanded on a lot! 这可以扩展很多! Including the fact that many packages have been created to accomplish this (eg. subdomain ) Disclaimer you may need to account for the use of www. 包括已经创建了许多软件包来完成此操作的事实(例如subdomain )。免责声明您可能需要考虑www.的使用www. with some urls. 与一些网址。

So I would use something like 所以我会用类似

router.get('/myRoute', function(req, res,next) {
  req.headers.host == ":param.localhost.com"
  //rest of code
}

I think I understand what you are saying, but I will do some testing and some further reading upon the headers of my request. 我想我理解您在说什么,但是我将对我的请求标题进行一些测试和进一步的阅读。

EDIT: Right now it seems like an unnecessary hassle to continue with this because I am also working with React-router at the moment. 编辑:现在看来继续这样做似乎是不必要的麻烦,因为此刻我也在使用React-router。 So for the time being I am just going to use my params after the /. 所以暂时我只是在/之后使用我的参数。 I thank you for your time and your answers. 感谢您的时间和回答。 Have a nice day! 祝你今天愉快!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM