1

suppose that item Id is "8j11r05s16ni7z2f-52592443542" and we are able to access this https://domainname.com/8j11r05s16ni7z2f-52592443542 but in my project, I have to expose item id as a subdomain like

https://8j11r05s16ni7z2f-52592443542.domainname.com

How to achieve in node js?

1 Answers1

0

I suggest you one of the approaches:

The webser config should capture subdomain like a param and paste it url segment.

Such web server layer from the second step can be written in node by yourself, can be taken from any ready package on npm (standalone or middleware, for example https://www.npmjs.com/package/http-proxy-middleware) or you can use common solution, like nginx, apache or other.

I recommend to use nginx. If you want so, here are the docs about how it can be achieved: https://docs.nginx.com/nginx/admin-guide/web-server/reverse-proxy/

https://web.archive.org/web/20111119030118/http://wiki.nginx.org/HttpProxyModule#proxy_pass

How can query string parameters be forwarded through a proxy_pass with nginx? - here you can find capturing examples

You setup reverse proxy, handle capturing id in proxy_pass section and add a captured param in its body

As for the pure node solution without separate web server, you should make very similar operation: serve all subdomains, handle root route, capture subdomain like a param and either proxy to route from the step 1, either call the logic directly with an id param.

Here is how to get domain in such root route controller: How to get the full URL in Express?

Eugene Mihaylin
  • 1,736
  • 3
  • 16
  • 31