diff --git a/README.md b/README.md new file mode 100644 index 0000000..256245a --- /dev/null +++ b/README.md @@ -0,0 +1,57 @@ +nowww +=== + +Redirects any domain with `www` to the same site without it. + + * `www.foobar3000.com` -> `foobar3000.com` + * `www.helloworld3000.com` -> `helloworld3000.com` + +See [no-www.org][no-www.org] ![no-www.org][no-www.ico] + + [no-www.ico]: http://no-www.org/images/blog-button.gif + [no-www.org]: http://no-www.org + +In short: + + All domains should have a `www` for backwards compatibility with what +early adopters of the internet have come to expect (and ctrl+enter adds it). +However, those domains should redirect to the root of the domain. + + * it means we type four fewer charaters + * we don't type `http://` anymore, why would we type `www.`? + * it's what the cool kids do (i.e. github) + * `ftp`, `irc`, `ssh`, etc all have their own *protocols*. Why should the web also have a prefix? + +Installation +=== + +```bash +npm install nowww +``` + +Usage +=== + +```javascript +(function () { + 'use strict'; + + var http = require('http') // (or https / spdy) + , connect = require('connect') // or express + , nowww = require('./') + , app = connect() + , server + ; + + app + .use(nowww()) + .use(require('serve-static')(__dirname + '/public/')) + ; + + server = http.createServer(); + server.on('request', app); + server.listen(3000, function () { + console.log('Listening on ' + server.address().port); + }); +}()); +``` diff --git a/example.js b/example.js new file mode 100644 index 0000000..0c96310 --- /dev/null +++ b/example.js @@ -0,0 +1,19 @@ +'use strict'; + +var http = require('http') // (or https / spdy) + , connect = require('connect') // or express + , nowww = require('./') + , app = connect() + , server + ; + +app + .use(nowww()) + .use(require('serve-static')(__dirname + '/public/')) + ; + +server = http.createServer(); +server.on('request', app); +server.listen(3000, function () { + console.log('Listening on ' + server.address().port); +}); diff --git a/index.js b/index.js new file mode 100644 index 0000000..efa7236 --- /dev/null +++ b/index.js @@ -0,0 +1,39 @@ +/*jshint strict:true node:true es5:true onevar:true laxcomma:true laxbreak:true eqeqeq:true immed:true latedef:true undef:true unused:true*/ +(function () { + "use strict"; + + function nowww(req, res, next) { + var host = (req.headers.host||'').replace(/^www\./, '') + , hostname = host.split(':')[0] + , protocol = 'http' + (req.connection.encrypted ? 's' : '') + '://' + , href = protocol + host + req.url + ; + + if (host === req.headers.host) { + return next(); + } + + // Permanent Redirect + res.statusCode = 301; + res.setHeader('Location', href); + // TODO set token (cookie, header, something) to notify browser to notify user about www + res.write( + 'Quit with the www already!!! It\'s not 1990 anymore!' + + '
' + + '' + hostname + '' + + '
NOT www.' + hostname + + '
NOT ' + protocol + hostname + + '
just ' + hostname + ' !!!' + + '
' + + ';-P' + ); + res.end(); + + } + + function create() { + return nowww; + } + + module.exports = create; +}()); diff --git a/package.json b/package.json new file mode 100644 index 0000000..fbc4e86 --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ "name": "nowww" +, "description": "Node.JS Connect module for no-www redirection" +, "author": "AJ ONeal (http://coolaj86.com)" +, "url": "http://github.com/coolaj86/connect-nowww" +, "repository": { + "type": "git" + , "url": "git@github.com:coolaj86/connect-nowww.git" + } +, "keywords": ["nowww", "no-www", "connect", "express", "redirct", "yes-www"] +, "contributors": [] +, "dependencies": { + } +, "main": "index" +, "version": "1.1.3" +}