How to route a subdomain to a static file?

511 views
Skip to first unread message

rvi...@reflectorentertainment.com

unread,
Feb 12, 2019, 4:58:33 PM2/12/19
to NodeChef
Hi,

I'd like to set up my website so that subdomain.domain.com points to root/subdomain/index.html

Is that possible with Nodechef? or do I need to change some settings in my DNS?

Thanks.

in...@nodechef.com

unread,
Feb 13, 2019, 7:26:35 AM2/13/19
to NodeChef
It depends. If you uploaded "roots/subdomain/index.html" as a static file, then you can click on App actions > Routing and then specify a route to map "/" ---> "roots/subdomain/index.html".
However, if these files are part of your application bundle, then you must perform the routing in your application layer since the proxy will not have access to them. Hope this helps.

rvi...@reflectorentertainment.com

unread,
Feb 13, 2019, 10:30:05 AM2/13/19
to NodeChef
Thanks for the reply. I have the files uploaded as static files, however if I just do the route like you mentioned, won't going to domain.com also get me that same file?
To be clear, this is the setup I'd like:

https://domain.com           -> /index.html
https
://subdomain.domain.com -> /subdomain/anotherindex.html

both html files are uploaded as statics with that same folder structure.

in...@nodechef.com

unread,
Feb 14, 2019, 4:40:05 PM2/14/19
to NodeChef
You are right.

The suggestion i made will not be adequate for your use case. You can handle this in your code though. Basically you can check the domain yourself and then perform the routing.

Basically in your cloud folder, you can have a public folder inside of it. So for example.

-- Cloud
---- Public
         index.html
         subdomain
              anotherindex.html

In your cloud code main.js file you coulkd have something like this.

app.get('/', function(req, res){
  if(req.get('host') == 'domain.com') {
     res.sendfile('/bundle/cloud/public/index.html'); 
  }
 else {
     res.sendfile('/bundle/cloud/public/subdomain/anotherindex.html')
 }
}); 

rvi...@reflectorentertainment.com

unread,
Feb 14, 2019, 4:52:31 PM2/14/19
to NodeChef
Oh, that's interesting.
Does that mean that I could basically put all my static files in my cloud/public/ and upload all of it (including the cloud code) with nc deploy --ccode -i <appname>? Will that get me the same kind of performance as uploading static files with the nc deploy --statics -i <appname> method?

in...@nodechef.com

unread,
Feb 14, 2019, 10:11:52 PM2/14/19
to NodeChef
There is a small round trip cost but it should be negligible. Yes when you put the public under the cloud folder, everything will be uploaded to the server together with your cloud code. The cloud folder can contain any asset required to be served in the app.

rvi...@reflectorentertainment.com

unread,
Feb 15, 2019, 7:35:58 PM2/15/19
to NodeChef
Thank you, I'll do that!

rvi...@reflectorentertainment.com

unread,
Feb 19, 2019, 7:57:42 PM2/19/19
to NodeChef
Hi again,

So, after reading more on this topic, I realised I can do a whole bunch more using express. I know about web design but I'm new to web apps!

Here's my goal:
  1. For domain.com, serve static files from /public/main/
  2. For sub.domain.com, serve static files from /public/sub/
  3. For sub.domain.com/resetpassword, send to sub.domain.com?resetpw=true (and add any pre-existing parameters)
I think code-wise I'm almost there. Here's what I have:

app.all('*', (req, res) => {

 
// Find out if we are requesting the subdomain
 
if ( ( req.subdomains || [false] )[0] === 'sub' ) {

   
// If we are, and the path is /resetpassword, then add resetpw=true to the URL params (flattening the preexisting ones into a new string)
   
if ( req.path === 'resetpassword' ) {
      res
.location('https://sub.domain.com/' + '?resetpw=true' + Object.keys(req.query).map(k => k + '=' + req.query[k]).join('&'));
   
}

   
// Then serve static files from the sub directory
    app
.use(express.static(__dirname + 'public/sub', { extensions: ['html', 'htm'] }));
 
} else {

   
// Otherwise, serve static files from the main directory
    app
.use(express.static(__dirname + 'public/main', { extensions: ['html', 'htm'] }));
 
}
});

Looking at the app logs, I feel like most of it works, but express.static can't be used because express isn't defined. I only have access to the app instance. How do I fix this?

Cheers. Let me know if I should create a new thread for this, since it's a bit of a deviation, or extension, of the initial topic.

PS: I think there should be a page on the documentation about this! I looked at https://www.nodechef.com/docs/parse-server/express but it didn't help, and https://www.nodechef.com/docs/parse-server/parse-server-local-example showed promise but the link to the example js file is broken.

in...@nodechef.com

unread,
Feb 20, 2019, 11:33:03 AM2/20/19
to NodeChef
Can you comfirm you have require('express') before attempting to use express. That typically goes at the top of your script file.

rvi...@reflectorentertainment.com

unread,
Feb 21, 2019, 11:58:08 AM2/21/19
to NodeChef
I... hadn't, because I got confused by "Do not create a new instance of express but use this global variable instead" in the docs. I've added that in.

...But now I'm facing a range of other issues :(

I created a question on SO, in case you want to have a look. Feel free to answer there or here, it would be greatly appreciated... I'm a bit lost!

rvi...@reflectorentertainment.com

unread,
Feb 25, 2019, 5:46:34 PM2/25/19
to NodeChef
Update, I've mostly gotten it to work using the following code:

const baseUrl = '/bundle/cloud/public/',
  vhost
= require('vhost'),
  express
= require('express'),
  routerMain
= express.Router(),
  routerSub
= express.Router(),
  staticOpts
= { extensions: ['html', 'htm'] };
 
//sub.domain.com
routerSub
.use((req, res, next) => {
 
// subdomain-specific logic
 
next();
});
routerSub
.use(express.static(baseUrl + 'sub/', staticOpts));
 
// domain.com
routerMain
.get('/sub', (req, res) => {
  res
.redirect('https://sub.domain.com/');
});
routerMain
.use(express.static(baseUrl, staticOpts));
 
// Connect routers
app
.use(vhost('sub.domain.com', routerSub));
app
.use(routerMain);

There's still one small issue, which is that a complete path to a file eg. domain.com/sub/file.html serves that file instead of redirecting first to sub.domain.com/file, but that's not a huge deal.
Reply all
Reply to author
Forward
0 new messages