Skip to content

Instantly share code, notes, and snippets.

@NuckChorris
Created March 20, 2011 09:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NuckChorris/878244 to your computer and use it in GitHub Desktop.
Save NuckChorris/878244 to your computer and use it in GitHub Desktop.
var http = require('http');
var https = require('https');
var ejs = require('ejs');
var pages = {
chat: {
list: '<% var rooms = [{owner:\'nuck\',name:\'chat:GoNuckYourself\',desc:\'Just go nuck yourself.\'},{owner:\'botdom\',name:\'chat:botdom\',desc:\'The home for bots on dAmn!\'}];%><html><head><title>dAmnode :: Chat List</title></head><body><center><table><tr><th>Chatroom Name</th><th>Creator</th><th>Description</th></tr><%for(var i = 0, l = rooms.length; i < l; i++) {var room = rooms[i].name.replace(\'chat:\',\'\');%><tr><td style="width:220px"><strong><a class="a" href="/chat/<%= room %>">#<%= room %></a></strong><br><span style="color:#6C7F77">10 deviants chatting</span></td><td style="padding-left:0; width:160px">$<a class="u" href="/user/<%= rooms[i].owner %>"><%= rooms[i].owner %></a></td><td colspan="2"><%= rooms[i].desc %></td></tr><% } %></table></center></body></html>',
},
'': '<center><h1>Coming Soon: Nuck\'s dAmn Server!</h1></center>'
};
var cb = function (req, res) {
req.setEncoding("utf8");
req.content = '';
try {
var path = require('url').parse(req.url);
try {
var page = path.pathname.split('/').slice(1).reduce(function (node, key) { return node[key] }, pages);
} catch (e) {
res.write(ejs.render(pages['404'] || '404: File Not Found'));
res.end();
}
if ( req.method == 'POST' ) {
req.addListener("data", function(chunk) {
req.content += chunk;
});
req.addListener("end", function(){
var postdata = require('querystring').parse( req.content );
if ( typeof page == 'function' ) {
page.apply(this, [req, res]);
} else {
res.write(ejs.render(page,{
'locals': {
'post': postdata
}
}));
res.end();
}
}.bind(this));
} else if ( req.method == 'GET' ) {
var get = require('querystring').parse( path.query );
if ( page !== undefined ) {
if ( typeof page == 'function' ) {
page.apply(this, [req, res]);
} else {
res.write(ejs.render(page,{
'locals': {
'get': get
}
}));
res.end();
}
} else {
if ( typeof pages['404'] == 'function' ) {
pages['404'].apply(this, [req, res]);
} else {
res.write(ejs.render(pages['404'] || '404: File Not Found'));
res.end();
}
}
}
} catch (e) {
res.write(ejs.render(pages['504'] || '504: Internal Server Error'));
res.end();
}
};
var server = http.createServer(cb);
console.log('HTTP Server Started');
var secure = https.createServer({
key: fs.readFileSync('nuck.no.de-key.pem'),
cert: fs.readFileSync('nuck.no.de-cert.pem')
},cb);
console.log('Secure Server Started');
server.listen(process.env.PORT || 8001);
console.log('HTTP Server listening on port ' + process.env.PORT);
secure.listen(443);
console.log('Secure Server listening on port 443');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment