Skip to content

Instantly share code, notes, and snippets.

@amokan
Forked from kieran/server.js
Created March 13, 2012 23:35
Show Gist options
  • Save amokan/2032679 to your computer and use it in GitHub Desktop.
Save amokan/2032679 to your computer and use it in GitHub Desktop.
basic Barista example
var Router = require('barista').Router // <-- require the router
, http = require('http')
, util = require('util')
var router = new Router // <-- create the router
// define some routes
router.get('/').to('app.index')
router.resource('products')
router.resource('users')
// create the HTTP server
var server = http.createServer()
// attach the request handler
server.on('request',function(req,res){
// when the request is done coming in, let's dispatch it
req.addListener('end', function (){
// does the router have a matching route?
var params = router.first(req.url, req.method) // <-- resolve the url & method to a params object
if (params) { // yes, dispatch
console.log('Routed to ' + params.controller + ' controller, ' + params.action + ' action')
res.end('All params: ' + JSON.stringify( params ) )
} else { // nope, 404
console.log('no routes match a request for '+req.url)
res.end('four-oh-four!')
}
}) // req 'end' event
}) // server 'request' event
// bind the server to port 8888
server.listen(8888)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment