Handle POST With Fabjs
Handle POST Method in Fab Javascript Framework (Fabjs)
Introduction
Fabjs is a modular async web framework for node.js. Fab is basically a framework that glues together functions. Based on the arity of the function. It definitely took a couple of days to wrap my head around the basics. Check out Chris Strom Getting Started Articles, they definitely help me understand things a little better.
NOTE I am leaving out the fab boilerplate for the examples in this article.
Simple Unary Call
If I just want to respond to a request such as /hello, then I would create a function like so:
(/^\/hello/ )
(function() {
this({body: "Hello World!"})()
})Handling POST Method
However, what if I wanted to only allow POST requests. I see that the Fab library has a fab.method.POST function, but how do we use this function. There's some simple documentation that states that fab.method.POST is a "Arity 3" or "Ternary" function. So, we need to provide two functions, one for success and one for failure (or in Fabjs terms hit or miss). Let's write that function:
(/^\/onlypost/)
(fab.method.POST)
(function() { this({body: "success"})() })
(function() { this({body: "failure"})() })When you hit the url /onlypost with a GET request, you will receive a document with a body of "failure". If you hit the url with a POST request, you will receive a document with a body of "success".
According to the Fabjs maintainers, you will not need to manually close the functions in upcoming releases. So the above would become:
(/^\/onlypost/)
(fab.method.POST)
(function() { this({body: "success"}) })
(function() { this({body: "failure"}) })And if you want to keep the connection open, you would use this.call(fn, body).
It took me about a day to figure this out, but I did start to feel more comfortable with the Fab framework.
Comments [0]