Search code examples
node.jsexpresskraken.js

How to assign a subdomain to some routes?


I'm writing an application in Node.js and express, this application has a frontend written in jade and less and we are also writing a RESTful API to create, read, update and delete data in the database (MongoDB)

For this application we have a domain: example.com, and we want all the frontend can be accessed from example.com and secondly we want only the API is accessible from api.example.com

How can we do so that only routes from the subdomain api respond?

PS. If not relevant, but the entire application was written using Kraken.js (PayPal)


Solution

  • What I'd recommend is to have two separate projects entirely: one for your web front-end, and one for your API service. Keep these two projects in separate Git repositories (or whatever form of version control you use), and give each project it's own database / web servers / etc.

    Once you've got two projects deployed to two (or more) servers, you can then configure your DNS to point to you services pretty easily.

    For your main site, you'll want to create a DNS A record that points to the public IP address of your web server that hosts your website project.

    For your API site, you'll want to create a DNS CNAME record that points to the web server that is hosting your API project.

    If you're using a service like Heroku, this process is even simpler: you can use CNAMEs for everything and simplify the process a bit.