use dynamic subdomains with nodejs

♀尐吖头ヾ 提交于 2020-05-29 10:08:29

问题


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.


回答1:


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', ...



来源:https://stackoverflow.com/questions/40174490/use-dynamic-subdomains-with-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!