-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Handl "any" subdomain #33
Comments
@VALIKCOOL try |
that did the trick. I think it should be documented |
@bmullan91 Hmm, for me that does not work... I need to have const express = require('express');
const bodyParser = require('body-parser');
const subdomain = require('express-subdomain');
const siteRouter = require('./src/routes/site');
const app = express()
app.use(express.json() );
app.use(express.urlencoded());
app.use(express.static('public'));
app.use(subdomain('*.www', siteRouter));
app.get('/', function(req, res) {
res.send('Homepage');
});
const server = app.listen(80,'x3.loc', function () {
var host = server.address().address;
var port = server.address().port;
console.log('X3 listening at http://%s:%s', host, port);
}); also, siteRouter file: const express = require('express');
let router = express.Router();
router.get('/', function(req, res) {
res.send('Welcome to site');
});
module.exports = router; |
Hi friends i got solution for dynamic domain to work locally: Use nginx as a server! If use using apache server, stop that and install nginx 🔥 first add all your domains and subdomains in /etc/hosts: (without this you can't able to make work in local system) 127.0.0.1 github.com edit your nginx.conf file: Set a nginx reverse proxy Add your node js project port, in my project i am using 5000, so i put 5000
Instead of app.use(subdomain('yourstuff', router)); // static way // get your dynamic subdomain
app.use(function(req, res, next) {
if (!req.subdomains.length || req.subdomains.slice(-1)[0] === 'www') return next();
// otherwise we have subdomain here
var subdomain = req.subdomains.slice(-1)[0];
// keep it
req.subdomain = subdomain;
next();
});
// render a page
app.get('/', function(req, res) {
// no subdomain
if (!req.subdomain) {
// render landing page
res.render('home');
} else {
// render subdomain specific data
res.render('user-page');
}
}); @VALIKCOOL @randohinn |
@SameerCodes - this is exactly what I did! Prety cool, good job for figuring this out as well. |
Thanks @knoxcard 😁 |
how to handle xxx.com (no subdomain) |
Hey! I recently discovered this great plugin, but i am facing an issue now. I want to handle "any" subdomain. For example, I need to have
sub1.example.com
sub2.example.com
sub3.example.com
and all of them should be handled by one handler. I have tried
app.use(subdomain("*", myrouter))
but with no luck. Only possible solution is to defined parent subdomain and catch all sub-sub domains, but it is not what i want to have.
The text was updated successfully, but these errors were encountered: