I'm trying to develop my app locally with subdomains, specifically trying to access something like test.localhost:3002, but I'm hitting a network error when I try to make API requests to my Fastify server. How do others manage local development with subdomains while using a Fastify server? Any tips would be really appreciated!
3 Answers
Just a heads up, using 'localhost' as a subdomain isn't entirely valid. You can run your Fastify server on different ports instead of trying to access them as subdomains. Each app can run on something like localhost:3001, localhost:3002, etc.
Hey! So, the first thing to know is that for subdomains to work on localhost, you need to tweak your 'hosts' file. On Windows, you can find it at c:windowssystem32driversetchosts, and on Mac or Linux, it’s usually /etc/hosts. Open it with admin rights and add this line:
`127.0.0.1 test.localhost`
Don’t forget to save your changes after that! Once you do this, try accessing test.localhost again. Let me know how it goes!
Just to double check, have you restarted your server after making those changes?
Thanks for the tip! I’ve added that line, but I'm still facing the same issue. Do you have any more suggestions?
Your localhost setup doesn't automatically support subdomains. To get it working, configure your DNS or directly modify the 'hosts' file as mentioned earlier. I also use valet-linux for managing local domains, but I'm not sure how it plays with Fastify specifically. It's super handy though!
That sounds cool! Is there a similar tool for Mac? I’d love to manage local domains more easily.
But isn’t it possible to get subdomains working through DNS?