An example app to show Transit. It requires Node with Harmony support. Only development version support Harmony at the moment, e.g. 0.11.14.
Filip Zrůst - Transferring application data efficiently with Transit: https://speakerdeck.com/frzng/transferring-application-data-efficiently-with-transit
git clone https://github.com/frzng/attendees.js.git
See the simple app with pure JSON REST API:
git checkout step-1-simple
Check the source code of index.js and try it yourself. Run the app in one terminal:
npm install
npm start
And test it in other terminal. Get list of all attendees:
curl -s localhost:3000/attendees
[]
Add a new attendee:
curl -i -H 'Content-Type: application/json' -d '{ "email": "frzng@me.com", "name": "Filip Zrůst" }' localhost:3000/attendees
HTTP/1.1 201 Created
Location: /attendees/0
Get list of all attendees again:
curl -s localhost:3000/attendees
[{
"email" : "frzng@me.com",
"name" : "Filip Zrůst"
}]
You should have a clear understanding of we're trying to achieve.
Storing data in global variable (not persistent). Data are attendee
records with email
and name
keys.
Don't forget to kill the server when moving to the next step.
Now with Transit:
git checkout step-2-transit
Compare changes from step 1 to step 2. and try it yourself. Run the app in one terminal:
npm install
npm start
If you want to see how Transit uses JSON-Verbose for debugging and how it looks, you can start the server this way:
npm start -- -v
During the initialization, it will output its in-memory database of attendees in JSON-Verbose. It doesn't respond to or expect requests in that format.
And test it in other terminal. Get list of all attendees:
curl -s localhost:3000/attendees
[[
"^ ",
"~:name", "Filip Zrůst",
"~:email", "~rmailto:frzng@me.com"
], [
"^ ",
"^0", "Stojan Jakotyč",
"^1", "~rmailto:sj@example.com"
]]
See how plain JavaScript object (i.e., map
Transit semantic type)
gets encoded to JSON array
element with the "^ "
tag indicating
that it is in fact a map. You can also scalar elements with :
and
r
tags for keyword
and URI
respectively.
Also notice how caching works for map keys.
Don't forget to kill the server when moving to the next step.
Note: This step doesn't properly support create
and update
actions. This is because co-body
should be replaced with Transit
reader.
Now with custom composite type writer for Attendee
record:
git checkout step-3-custom
Compare changes from step 2 to step 3. and try it yourself. Run the app in one terminal:
npm start
And test it in other terminal. Get list of all attendees:
curl -s localhost:3000/attendees
[[
"~#att", [
"^ ",
"~:name", "Filip Zrůst",
"~:email", "~rmailto:frzng@me.com"
]
], [
"^0", [
"^ ",
"^1", "Stojan Jakotyč",
"^2", "~rmailto:sj@example.com"
]
]]
See the custom tag att
for composite element and that even the tag
gets cached (if it is longer than three characters).
Don't forget to kill the server when moving to the next step.
Note: This step doesn't properly support create
and update
actions. This is because co-body
should be replaced with Transit
reader.
Now with custom composite type reader for Attendee
record:
git checkout step-4-reader
Compare changes from step 3 to step 4. and try it yourself. Run the app in one terminal:
npm install
npm prune
npm start
And test it in other terminal. Get list of all attendees:
curl -s localhost:3000/attendees
[[
"~#att", [
"^ ",
"~:name", "Filip Zrůst",
"~:email", "~rmailto:frzng@me.com"
]
], [
"^0", [
"^ ",
"^1", "Stojan Jakotyč",
"^2", "~rmailto:sj@example.com"
]
]]
Add a new attendee:
curl -i -H 'Content-Type: application/transit+json' -d '[ "~#att", [ "^ ", "~:name", "Tomáš Marný", "~:email", "~rmailto:sj@example.com" ] ]' localhost:3000/attendees
HTTP/1.1 201 Created
Location: /attendees/2
Get list of all attendees again:
curl -s localhost:3000/attendees
[[
"~#att", [
"^ ",
"~:name", "Filip Zrůst",
"~:email", "~rmailto:frzng@me.com"
]
], [
"^0", [
"^ ",
"^1", "Stojan Jakotyč",
"^2", "~rmailto:sj@example.com"
]
], [
"^0", [
"^ ",
"^1", "Tomáš Marný",
"^2", "~rmailto:sj@example.com"
]
]]
Don't forget to kill the server when leaving…