10

I'd like to share some user information at username.domain.com at my application. Subdomain should be available after user create his account.

I have found nice module that could be useful in that case: Express Subdomain

How can I do it properly using that module? Maybe this module isn't so useful so which one should I use?

Abdizriel
  • 345
  • 1
  • 4
  • 20
  • 1
    I would recommend you using Nginx in front of Node. Nginx allows you to handle subdomains without any problems. – Oleg Jun 20 '15 at 07:41
  • Besides, starting webserver requires listening 80 port, which in turn requires root privileges. Starting Node with root privileges is a bad practice because of security consideration. But you can (and should) start Nginx as root, and Node as regular user. – Oleg Jun 20 '15 at 07:47

1 Answers1

34

As I mentioned in OP comments, using Nginx webserver in front of Node would be very good option, since this is a secure way to listen 80 port. You can also serve static files (scripts, styles, images, fonts, etc.) more efficiently, as well as have multiple sites within a single server, with Nginx.

As for your question, with Nginx, you can listen both example.com and all its subdomains, and then pass subdomain to Node as a custom request header (X-Subdomain).

example.com.conf:

server {
    listen          *:80;
    server_name     example.com   *.example.com;

    set $subdomain "";
    if ($host ~ ^(.*)\.example\.com$) {
        set $subdomain $1;
    }

    location / {
        proxy_pass          http://127.0.0.1:3000;
        proxy_set_header    X-Subdomain     $subdomain;
    }
}

app.js:

var express = require('express');
var app = express();

app.get('/', function(req, res) {
    res.end('Subdomain: ' + req.headers['x-subdomain']);
});

app.listen(3000);

This is a brief example of using Nginx and Node together. You can see more detailed example with explanation here.

Community
  • 1
  • 1
Oleg
  • 22,300
  • 9
  • 68
  • 84
  • 1
    How I can then set content of subdomain by user od details? – Abdizriel Jun 21 '15 at 11:20
  • @Abdizriel I'm not sure i understood you correctly. can you please describe in details what you want to achieve? – Oleg Jun 21 '15 at 11:34
  • for example I've user with username testuser who have name Test and last name User. When someone go to page testuser.example.com he/she would see: Welcome Test User This is simple example, what Iwan to do is more complicated. – Abdizriel Jun 22 '15 at 06:44
  • @Abdizriel just keep users' data in DB. For MySQL, your query might look like: `SELECT first_name, last_name FROM users WHERE username=?`. where `?` - is your subdomain taken from `req.headers['x-subdomain']` – Oleg Jun 22 '15 at 07:01
  • An addendum to what @Oleg said, you will ideally want to have some stored procedure that performs the database query to protect from SQL injection since you have user data/accounts. No matter how simple a static page is, your back end should be protected. Should have a simple API on your domain that can be called on by the subdomain to query the database, instead of directly querying the database via a function in the subdomain. – jon.bray.eth Oct 28 '20 at 21:02