Created
November 10, 2010 06:48
-
-
Save localshred/670464 to your computer and use it in GitHub Desktop.
Experimentations in sinatra-style node.js
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 util = require('util'), | |
http = require('http'), | |
router = require('./router'); | |
console.log('creating server'); | |
var app = http.createServer(); | |
console.log('registering app'); | |
router.registerApp(app); | |
console.log('loading controller'); | |
var controller = require('./controller'); | |
console.log('Server running at http://127.0.0.1:8124/'); | |
app.listen(8124, "127.0.0.1"); |
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 router = require('./router'); | |
var util = require('util'); | |
router.get('/', function(params) { | |
return '<form action="/booty" method="post">'+ | |
'<input type="text" name="snooty" value="Snooty 1">'+ | |
'<input type="text" name="snooty" value="Snooty 2">'+ | |
'<input type="text" name="snooty" value="Snooty 3">'+ | |
'<input type="text" name="snooty" value="Snooty 4">'+ | |
'<input type="text" name="snooty" value="Snooty 5">'+ | |
'<input type="text" name="snooty" value="Snooty 6">'+ | |
'<input type="text" name="snooty" value="Snooty 7">'+ | |
'<input type="text" name="snooty" value="Snooty 8">'+ | |
'<input type="submit" value="Go to Booty">'+ | |
'</form>'; | |
}); | |
router.post('/booty', function(params) { | |
console.log(util.inspect(params)); | |
return 'Booty booty boot boot ' + params.snooty[0]; | |
}); |
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 querystring = require('querystring'); | |
var routes = { | |
'GET': {}, | |
'POST': {}, | |
'PUT': {}, | |
'DELETE': {} | |
}; | |
function defineRoute (method, url, route) { | |
routes[method][url] = route; | |
}; | |
exports.invoke = function (req, res) { | |
var route = routes[req.method][req.url]; | |
function writeResponse(req, res, code, body) { | |
console.log(req.method + ' ' + req.url + ' (' + code + ')'); | |
res.writeHead(code, {'Content-Type': 'text/html'}); | |
res.write(body + '\n'); | |
res.end(); | |
} | |
if (route) { | |
var requestBody = ''; | |
req.on('data', function(chunk) { | |
requestBody += chunk.toString(); | |
}); | |
req.on('end', function(){ | |
var params = querystring.parse(requestBody); | |
var output = route(params); | |
writeResponse(req, res, 200, output) | |
}); | |
} | |
else { | |
writeResponse(req, res, 404, 'No route matches ' + req.url); | |
} | |
}; | |
exports.registerApp = function(app) { | |
app.on('request', exports.invoke); | |
}; | |
exports.get = function (url, route) { | |
defineRoute('GET', url, route); | |
}; | |
exports.post = function (url, route) { | |
defineRoute('POST', url, route); | |
}; | |
exports.put = function (url, route) { | |
defineRoute('PUT', url, route); | |
}; | |
exports.delete = function (url, route) { | |
defineRoute('DELETE', url, route); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment