Effects
Last updated
Last updated
Effect is the main building block of the whole framework. Using its generic interface we can define API endpoints (so called: Effects), and (see next chapters).
The simplest implementation of API endpoint can look like this:
The sample Effect above resolves every HTTP request that matches to root /
path and GET method type and at the end responds with Hello, world!
message. Simple as hell, right?
Every API Effect request has to be mapped to object which can contain attributes like body
, status
or headers
. If status code or headers are not passed, then API by default will respond with 200 status and application/json
Content -Type header.
A little bit more complex example can look like this:
url
method
The example above will match every POST request that matches to /user
url. Using previously parsed POST body (see middleware) we can map it to sample DAO which returns a response
object as an action confirmation.
Every Effect has an access to two most basics objects created by http.Server. The first one is HttpRequest which is just an abstraction over basic Node.js object. It may be used to access response status, headers and data, but in most scenarios you don't have to deal with all available APIs offered by IncomingMessage class. The most common properties available in request object are:
body (see section)
params (see chapter)
query (see chapter)
For more details about available API offered in http.IncomingMessage
, please visit .
Like previously described HttpRequest, the HttpResponse object is also just an abstraction over basic Node.js object. Besides the default API, the response object exposes an res.send
method, which can be a handy wrapper over Marble.js responding mechanism. For more detailed information about res.send method, visit chapter.
For more details about available API offered in http.ServerResponse
, please visit .