Redirecting from naked domain to www with node/express?
The group you are posting to is a
Usenet group . Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
From:
"Felix E. Klee" <felix.k... @inka.de>
Date: Mon, 4 Jun 2012 00:07:09 -0700 (PDT)
Local: Mon, Jun 4 2012 3:07 am
Subject: Redirecting from naked domain to www with node/express?
I am hosting an express.js application on nodejitsu.com. Now, I have
assigned two domain names to it:
example.com
www.example.com
This works (although, this morning there were some glitches). Now, I
would like to always redirect:
example.com -> www.example.com
example.com/some/page.html -> www.example.com/some/page.html
...
Is there an easy way to achieve that using routing from within
node/express?
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Alan Hoffmeister <alanhoffmeis... @gmail.com>
Date: Mon, 4 Jun 2012 09:31:58 -0300
Local: Mon, Jun 4 2012 8:31 am
Subject: Re: [nodejs] Redirecting from naked domain to www with node/express?
I think that the correct way is to asign a CNAME for "
www.example.com "
in your dns manager redirecting it to "example.com".
But yep, you can do this in express by writting a middleware that
detect the called host name and redirect the user if necessary.
You would be able to get more information at Express.js list:
https://groups.google.com/group/express-js
--
Alan Hoffmeister
2012/6/4 Felix E. Klee <felix.k... @inka.de>:
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
"Felix E. Klee" <felix.k... @inka.de>
Date: Mon, 4 Jun 2012 14:41:54 +0200
Local: Mon, Jun 4 2012 8:41 am
Subject: Re: [nodejs] Redirecting from naked domain to www with node/express?
On Mon, Jun 4, 2012 at 2:31 PM, Alan Hoffmeister
<alanhoffmeis
... @gmail.com> wrote:
> I think that the correct way is to asign a CNAME for "
www.example.com "
> in your dns manager redirecting it to "example.com".
Setting a CNAME should not make a difference to the current setup. I
already have identical A records for
www.example.com and example.com.
And this works. What I want, though, is redirection.
> But yep, you can do this in express by writting a middleware that
> detect the called host name and redirect the user if necessary.
Thanks for the suggestion! Perhaps there is already a middleware that
does this.
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Oliver Leics <oliver.le... @gmail.com>
Date: Mon, 4 Jun 2012 14:51:36 +0200
Local: Mon, Jun 4 2012 8:51 am
Subject: Re: [nodejs] Redirecting from naked domain to www with node/express?
It should be something like this:
express.use(function(req, res, next) {
if(req.headers.host === 'example.com') {
res.writeHead(303, {'Location': 'http://www.example.com'+req.url })
res.end()
}
})
I did not test this.
I'm not even using express :-D
But something like this should do the trick.
On Mon, Jun 4, 2012 at 2:41 PM, Felix E. Klee <felix.k... @inka.de> wrote:
> On Mon, Jun 4, 2012 at 2:31 PM, Alan Hoffmeister
> <alanhoffmeis
... @gmail.com> wrote:
>> I think that the correct way is to asign a CNAME for "
www.example.com "
>> in your dns manager redirecting it to "example.com".
> Setting a CNAME should not make a difference to the current setup. I
> already have identical A records for www.example.com and example.com.
> And this works. What I want, though, is redirection.
>> But yep, you can do this in express by writting a middleware that
>> detect the called host name and redirect the user if necessary.
> Thanks for the suggestion! Perhaps there is already a middleware that
> does this.
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
--
Oliver Leics @ G+
https://plus.google.com/112912441146721682527
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
"Felix E. Klee" <felix.k... @inka.de>
Date: Mon, 4 Jun 2012 15:43:05 +0200
Local: Mon, Jun 4 2012 9:43 am
Subject: Re: [nodejs] Redirecting from naked domain to www with node/express?
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
"Felix E. Klee" <felix.k... @inka.de>
Date: Mon, 4 Jun 2012 16:38:55 +0200
Local: Mon, Jun 4 2012 10:38 am
Subject: Re: [nodejs] Redirecting from naked domain to www with node/express?
On Mon, Jun 4, 2012 at 2:51 PM, Oliver Leics <oliver.le
... @gmail.com>
wrote:
> express.use(function(req, res, next) {
> if(req.headers.host === 'example.com') {
> res.writeHead(303, {'Location': '
http://www.example.com'+req.url })
> res.end()
> }
> })
That made me learn a little about middleware, a new concept to me, and I
really like it. In the end I implemented basically what you proposed,
plus `next()` in the else clause.
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Oliver Leics <oliver.le... @gmail.com>
Date: Mon, 4 Jun 2012 17:02:06 +0200
Local: Mon, Jun 4 2012 11:02 am
Subject: Re: [nodejs] Redirecting from naked domain to www with node/express?
On Mon, Jun 4, 2012 at 4:38 PM, Felix E. Klee <felix.k
... @inka.de> wrote:
> On Mon, Jun 4, 2012 at 2:51 PM, Oliver Leics <oliver.le
... @gmail.com>
> wrote:
>> express.use(function(req, res, next) {
>> if(req.headers.host === 'example.com') {
>> res.writeHead(303, {'Location': '
http://www.example.com'+req.url })
>> res.end()
>> }
>> })
> That made me learn a little about middleware, a new concept to me, and I
> really like it. In the end I implemented basically what you proposed,
> plus `next()` in the else clause.
Middleware is a huge word for such a small yet powerful interface.
And yes, the next() call was missing.
Most nodejs code does not use the else-clause, but early return
instead. Nicer to read, fewer to type and some benchmarks showed a
slight speed-improvement.
The code would then be like this:
express.use(function(req, res, next) {
if(req.headers.host === 'example.com') {
return res.send({'Location': 'http://www.example.com'+req.url }, 301)
}
next()
})
This should be the shortest implementation according to
http://expressjs.com/guide.html
and .send() of express.
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Tim Caswell <t... @creationix.com>
Date: Mon, 4 Jun 2012 10:07:14 -0500
Local: Mon, Jun 4 2012 11:07 am
Subject: Re: [nodejs] Redirecting from naked domain to www with node/express?
I will mention that most browsers send the port as part of the host header.
So if you're testing locally using a custom /etc/hosts on port 8080 (or
just using port 8080 on the production machine), you'll need to account for
that in the host routing.
On Mon, Jun 4, 2012 at 10:02 AM, Oliver Leics <oliver.le... @gmail.com>wrote:
> On Mon, Jun 4, 2012 at 4:38 PM, Felix E. Klee <felix.k
... @inka.de> wrote:
> > On Mon, Jun 4, 2012 at 2:51 PM, Oliver Leics <oliver.le
... @gmail.com>
> > wrote:
> >> express.use(function(req, res, next) {
> >> if(req.headers.host === 'example.com') {
> >> res.writeHead(303, {'Location': '
http://www.example.com'+req.url })
> >> res.end()
> >> }
> >> })
> > That made me learn a little about middleware, a new concept to me, and I
> > really like it. In the end I implemented basically what you proposed,
> > plus `next()` in the else clause.
> Middleware is a huge word for such a small yet powerful interface.
> And yes, the next() call was missing.
> Most nodejs code does not use the else-clause, but early return
> instead. Nicer to read, fewer to type and some benchmarks showed a
> slight speed-improvement.
> The code would then be like this:
> express.use(function(req, res, next) {
> if(req.headers.host === 'example.com') {
> return res.send({'Location': 'http://www.example.com'+req.url }, 301)
> }
> next()
> })
> This should be the shortest implementation according to
> http://expressjs.com/guide.html
> and .send() of express.
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines:
> https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nodejs@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
"Felix E. Klee" <felix.k... @inka.de>
Date: Mon, 4 Jun 2012 17:10:14 +0200
Local: Mon, Jun 4 2012 11:10 am
Subject: Re: [nodejs] Redirecting from naked domain to www with node/express?
On Mon, Jun 4, 2012 at 5:07 PM, Tim Caswell <t
... @creationix.com> wrote:
> So if you're testing locally [...]
Thanks for the suggestion. But that's not necessary: The code is for
production only, with the if clause checking the domain.
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
"Felix E. Klee" <felix.k... @inka.de>
Date: Mon, 4 Jun 2012 17:12:45 +0200
Local: Mon, Jun 4 2012 11:12 am
Subject: Re: [nodejs] Redirecting from naked domain to www with node/express?
On Mon, Jun 4, 2012 at 5:02 PM, Oliver Leics <oliver.le
... @gmail.com>
wrote:
> Nicer to read,
Matter of taste.
> fewer to type and some benchmarks showed a slight speed-improvement.
Well, I guess this can be optimized with tools such as the Closure
Compiler.
> express.use(function(req, res, next) {
> if(req.headers.host === 'example.com') {
> return res.send({'Location': '
http://www.example.com'+req.url }, 301)
> }
> next()
> })
Thanks!
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Oliver Leics <oliver.le... @gmail.com>
Date: Mon, 4 Jun 2012 17:50:49 +0200
Local: Mon, Jun 4 2012 11:50 am
Subject: Re: [nodejs] Redirecting from naked domain to www with node/express?
On Mon, Jun 4, 2012 at 5:07 PM, Tim Caswell <t
... @creationix.com> wrote:
> I will mention that most browsers send the port as part of the host header.
Good point. I'm not sure if this is the case for port 80 too, but
let's stay on the save side:
express.use(function(req, res, next) {
if(req.headers.host.split(':',1)[0] === 'example.com') {
return res.send({'Location': 'http://www.example.com'+req.url }, 301)
}
next()
})
And what about case sensitivity?
if(req.headers.host.split(':',1)[0].toLowerCase() === 'example.com')
-o
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
"Felix E. Klee" <felix.k... @inka.de>
Date: Mon, 4 Jun 2012 17:54:21 +0200
Local: Mon, Jun 4 2012 11:54 am
Subject: Re: [nodejs] Redirecting from naked domain to www with node/express?
Updated:
if (req.headers.host.split(':', 1)[0].toLowerCase() ===
'example.com') {
res.header('Location', 'http://www.example.com' + req.url);
return res.send(301);
}
next();
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Anthony Ettinger <ettin... @gmail.com>
Date: Mon, 8 Oct 2012 22:45:07 -0700 (PDT)
Local: Tues, Oct 9 2012 1:45 am
Subject: Re: Redirecting from naked domain to www with node/express?
I just created a module to do domain redirecting on npm: https://npmjs.org/package/express-force-domain
npm install express-force-domain
app.all('*', require('express-force-domain')('http://www.example.com' ) );
On Monday, June 4, 2012 12:07:09 AM UTC-7, Felix E. Klee wrote:
> I am hosting an express.js application on nodejitsu.com. Now, I have > assigned two domain names to it:
> example.com > www.example.com
> This works (although, this morning there were some glitches). Now, I > would like to always redirect:
> example.com -> www.example.com > example.com/some/page.html -> www.example.com/some/page.html > ...
> Is there an easy way to achieve that using routing from within > node/express?
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
"Felix E. Klee" <felix.k... @inka.de>
Date: Tue, 9 Oct 2012 21:50:55 +0200
Local: Tues, Oct 9 2012 3:50 pm
Subject: Re: [nodejs] Re: Redirecting from naked domain to www with node/express?
On Tue, Oct 9, 2012 at 7:45 AM, Anthony Ettinger <ettin
... @gmail.com>
wrote:
Cool!
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
Isaac Schlueter <i... @izs.me>
Date: Tue, 9 Oct 2012 13:12:07 -0700
Local: Tues, Oct 9 2012 4:12 pm
Subject: Re: [nodejs] Re: Redirecting from naked domain to www with node/express?
You can also use canonical-host for this:
https://npmjs.org/package/canonical-host
var canon = require("canonical-host")('https://hostname.com' )
app.use(canon)
It also supports specific port numbers, https/http, etc. That's what
I'm using to redirect npm.im to npmjs.org.
On Tue, Oct 9, 2012 at 12:50 PM, Felix E. Klee <felix.k... @inka.de> wrote:
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
"Felix E. Klee" <felix.k... @inka.de>
Date: Wed, 10 Oct 2012 16:21:06 +0200
Local: Wed, Oct 10 2012 10:21 am
Subject: Re: [nodejs] Re: Redirecting from naked domain to www with node/express?
On Tue, Oct 9, 2012 at 10:12 PM, Isaac Schlueter <i
... @izs.me> wrote:
> You can also use canonical-host for this: [...]
Thanks! It's good to know the options.
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
CoolAJ86 <coola... @gmail.com>
Date: Wed, 10 Oct 2012 11:13:55 -0700 (PDT)
Local: Wed, Oct 10 2012 2:13 pm
Subject: Re: [nodejs] Re: Redirecting from naked domain to www with node/express?
You must
Sign in before you can post messages.
You do not have the permission required to post.
From:
CoolAJ86 <coola... @gmail.com>
Date: Wed, 10 Oct 2012 11:14:48 -0700 (PDT)
Local: Wed, Oct 10 2012 2:14 pm
Subject: Re: [nodejs] Re: Redirecting from naked domain to www with node/express?
typo. should be
connect.use(nowww())
On Wednesday, October 10, 2012 12:13:55 PM UTC-6, CoolAJ86 wrote:
> var nowww = require('nowww');
> connect.use(nowww);
> I believe `express.use(nowww);` should work the same.
> https://npmjs.org/package/nowww
> https://github.com/coolaj86/steve/tree/master/connect-nowww
> On Wednesday, October 10, 2012 8:22:32 AM UTC-6, Felix E. Klee wrote:
>> On Tue, Oct 9, 2012 at 10:12 PM, Isaac Schlueter <i... @izs.me> wrote: >> > You can also use canonical-host for this: [...]
>> Thanks! It's good to know the options.
You must
Sign in before you can post messages.
You do not have the permission required to post.