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.gitSee the simple app with pure JSON REST API:
git checkout step-1-simpleCheck the source code of index.js and try it yourself. Run the app in one terminal:
npm install
npm startAnd 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": "[email protected]", "name": "Filip Zrůst" }' localhost:3000/attendeesHTTP/1.1 201 Created
Location: /attendees/0
Get list of all attendees again:
curl -s localhost:3000/attendees[{
"email" : "[email protected]",
"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-transitCompare changes from step 1 to step 2. and try it yourself. Run the app in one terminal:
npm install
npm startIf you want to see how Transit uses JSON-Verbose for debugging and how it looks, you can start the server this way:
npm start -- -vDuring 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:[email protected]"
], [
"^ ",
"^0", "Stojan Jakotyč",
"^1", "~rmailto:[email protected]"
]]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-customCompare changes from step 2 to step 3. and try it yourself. Run the app in one terminal:
npm startAnd test it in other terminal. Get list of all attendees:
curl -s localhost:3000/attendees[[
"~#att", [
"^ ",
"~:name", "Filip Zrůst",
"~:email", "~rmailto:[email protected]"
]
], [
"^0", [
"^ ",
"^1", "Stojan Jakotyč",
"^2", "~rmailto:[email protected]"
]
]]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-readerCompare changes from step 3 to step 4. and try it yourself. Run the app in one terminal:
npm install
npm prune
npm startAnd test it in other terminal. Get list of all attendees:
curl -s localhost:3000/attendees[[
"~#att", [
"^ ",
"~:name", "Filip Zrůst",
"~:email", "~rmailto:[email protected]"
]
], [
"^0", [
"^ ",
"^1", "Stojan Jakotyč",
"^2", "~rmailto:[email protected]"
]
]]Add a new attendee:
curl -i -H 'Content-Type: application/transit+json' -d '[ "~#att", [ "^ ", "~:name", "Tomáš Marný", "~:email", "~rmailto:[email protected]" ] ]' localhost:3000/attendeesHTTP/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:[email protected]"
]
], [
"^0", [
"^ ",
"^1", "Stojan Jakotyč",
"^2", "~rmailto:[email protected]"
]
], [
"^0", [
"^ ",
"^1", "Tomáš Marný",
"^2", "~rmailto:[email protected]"
]
]]Don't forget to kill the server when leaving…