Created
July 3, 2013 23:08
-
-
Save francescoagati/5923648 to your computer and use it in GitHub Desktop.
node.js + express.js + bacon.js -> wrap express.js in functional reactive programming
This file contains hidden or 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
express = require("express") | |
Bacon = require("baconjs") | |
app = express() | |
route = (app) -> (path) -> | |
bus = new Bacon.Bus() | |
app.get path, (req,res) -> | |
bus.push | |
req: -> req | |
res: -> res | |
bus | |
#app.get "/hello.txt", (req, res) -> | |
app2 = route(app) | |
r1 = app2('/hello/:a.txt') | |
incr = (n) -> Bacon.fromCallback (next) -> | |
fn = -> next(parseInt(n) + 1) | |
setTimeout fn,3000 | |
map_data = (con) -> | |
con.incr_a = incr con.req().params.a | |
Bacon.combineTemplate con | |
r1.filter( (con) -> con.req().params.a == '1' ).flatMap(map_data).log().onValue (con) -> con.res().end con.incr_a.toString() | |
r1.filter( (con) -> con.req().params.a != '1' ).onValue (con) -> con.res().end con.req().params.a | |
app.listen 3000 | |
console.log "Listening on port 3000" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment