Ritu Gupta
Ritu Gupta

Reputation: 515

use dynamic subdomains with nodejs

hello i am new to Nodejs. I want to use dynamic subdomains to access my API and through subdomain prefix I can manage my API data.

Suppose I gave domain like domain:3000 and sub-domain could be a.domain:3000 or b.domain:3000 or anything prefixed to domain:3000.

I used wildcard-domains. but still unable to undersatnd the flow and how to use it and allow organisation listed in DB (Consider prefix as organisation name).

I have used following code:

var wildcardSubdomains = require('wildcard-subdomains')
var checkUser = subdomain('*.localhost:3000', function(req, res, 
next) {

console.log(req.session.user.valid);
if(!req.session.user.valid) {
    return res.send('Permission denied.');
}
next();
});

app.use(checkUser);

I am also using angularjs and using ui.router to change my states or urls.

Upvotes: 3

Views: 5425

Answers (1)

Alexander
Alexander

Reputation: 31

I used this module

npm i vhost --save

Here you can see information http://expressjs.com/en/resources/middleware/vhost.html

wildcard-subdomains

As you can see in https://www.npmjs.com/package/wildcard-subdomains

app.use(wildcardSubdomains({
    namespace: 's', // __NAMESPACE_FROM_WILDCARD_CONFIG__
    www: 'false',
}))

If you follow, example, link foo.localhost:3000 Express will process this middleware

app.get('/s/foo/', function(req, res){
    res.send("Meow!")
})

That is to say

app.get('/__NAMESPACE_FROM_WILDCARD_CONFIG__/__SUBDOMAIN__/', function(req, res){
    res.send("Meow!")
})

You can try to write app.get('/s/:subdomain', ...

Upvotes: 3

Related Questions