Created
December 4, 2011 05:14
-
-
Save FugueNation/1429248 to your computer and use it in GitHub Desktop.
Super Simple Node.js server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var simpleServer = require('simpleServer'); | |
simpleServer({ | |
tiemout : 1000, | |
router : { | |
hello : { | |
world : function( data, client ) { | |
client.reply( null, { you : data.name } ); | |
}, | |
}, | |
generic : { | |
handle : function( data, client ) { | |
console.log( client.path, data ); | |
client.reply( null, data ); | |
}, | |
}, | |
}, | |
}).listen( 1337 ); | |
process.on('uncaughtException', function( err ) { | |
console.error( err.stack || err.message || err ); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require('http'); | |
var url = require('url'); | |
var assert = require('assert'); | |
module.exports = function ( options ) { | |
assert.ok( options.router, 'must define a router object' ); | |
var router = options.router; | |
var host = options.host || '0.0.0.0'; | |
var port = options.port || 80; | |
var timeout= options.timeout || 3000; | |
var logger = options.logger || function( err, answer, client ) { | |
var errMsg; | |
if ( err ) { | |
console.error( err.stack || err.message || errMsg ); | |
errMsg = 'server error'; | |
} | |
var end = new Date(); | |
var dateformat = require('dateformat'); | |
console.log( | |
dateformat( end, 'HH:MM:ss.l'), | |
client.url, | |
(errMsg && errMsg.replace(/\s+/g, '_')) || true, | |
end.getTime() - client.timestamp, | |
answer | |
); | |
} | |
reply = function( err, answer ) { | |
clearTimeout( this.timerId ); | |
this.res.end( JSON.stringify([err && (err.message || err), answer]), 'utf8' ); | |
logger( err, answer, this ); | |
} | |
return http.createServer( function handleRequest( req, res ) { | |
try { | |
req.res = res; | |
req.timestamp = new Date().getTime(); | |
var query = url.parse(req.url, true); | |
req.query = query; | |
var path = query.pathname.split('/'); | |
path.shift(); | |
req.path = path; | |
req.reply = reply; | |
req.timerId = setTimeout( req.reply, timeout, new Error( 'server timeout' ) ); | |
var route = router; | |
for( var r in path ) { | |
var step = route[path[r]]; | |
if ( typeof step === "object" ) { | |
if ( step.handle ) { | |
return step.handle( req.query.query, req ); | |
} | |
route = step; | |
} else if ( typeof step === "function" ) { | |
return step( req.query.query, req ); | |
} | |
} | |
req.reply( new Error('server unhandled request') ); | |
} catch ( err ) { | |
console.log( err.stack || err.message || err ); | |
req.reply( new Error('server error') ); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment