-
-
Save amokan/2032679 to your computer and use it in GitHub Desktop.
basic Barista example
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('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