Putting It All Together
Let's put together all the individual parts of the DARRT framework and run some simple queries.
Starting a local instance of the Onboarding API
Now that we’ve reviewed all the individual parts of the DARRT framework (data, actions, resources, representations, and transitions) and seen how it’s put together, we can run some simple queries against our instance of the Onboarding API to see how it behaves. To do that, we need to start up our local instance of the Onboarding API and then use cURL to execute some simple tests.
Running queries against the Onboarding API
First, to start the local instance of the Onboarding API, run the following command in the terminal below:
npm run dev
This command will fire up the Onboarding API service and wait for requests. It should look like:
> onboarding@1.0.0 dev /onboarding-api
> nodemon index
[nodemon] 2.0.18
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index index.js`
listening on port 8080!
To run some queries against our Onboarding API, we need to open another terminal. This is where we’ll use curl to send some requests to the API to inspect its responses.
Note: To open one more terminal, click the “
+
” icon on the terminal.
To start, let’s check out the root of the API service and see what’s returned. Type the following lines of code in the second terminal and view the response:
curl localhost:8080 \
-H "accept:application/json"
The response should be as follows:
{
"home" : [
{
"id" : "list",
"name" : "onboarding",
"rel" : "collection onboarding",
"href" : "http://localhost:8080/wip/"
}
] }
This is the root object, which is pointing us to the list
resource. Note the format is returned in plain JSON, the default for this API. We can alter the response format by passing the HTTP accept
header like this:
curl localhost:8080 \
-H "accept:application/forms+json"
Its response should look something like this:
{
"home" :
{
"metadata" : [
{"name" : "title","value" : "BigCo Onboarding Records"},
{"name" : "author","value" : "Mike Amundsen"},
{"name" : "release","value" : "1.0.0"}
],
"links" : [
{"id" : "self","name" : "self","href" : "http://localhost:8080/",
"rel" : "self collection onboarding",
"tags" : "collection onboarding self home list item",
"title" : "Self","method" : "GET","properties" : []
},
{"id" : "home","name" : "home","href" : "http://localhost:8080/",
"rel" : "collection onboarding",
"tags" : "collection onboarding home list item",
"title" : "Home","method" : "GET","properties" : []
},
{"id" : "list","name" : "list","href" : "http://localhost:8080/wip/",
"rel" : "collection onboarding",
"tags" : "collection onboarding home list item",
"title" : "List","method" : "GET","properties" : []
} ],
"items" : [] }
}
The application/forms+json
representation contains additional information, including not just href
values, but methods, descriptions, and when appropriate, request parameters (using the properties[]
collection). The good news is, when using a framework that supports varying representations and transitions (like DARRT does), developers can decide for themselves which response format they prefer to work with.
Terminal
Get hands-on with 1200+ tech skills courses.