This is an example implementation of moredip's Todo-Backend API spec, using TypeScript, Node.js and the Express framework.
This example saves TODOs in a PostgreSQL database and uses node-db-migrate for database migrations. The code can be deployed to heroku.
This code is based on the todo-backend-express example on the http://todobackend.com/ site.
> npm install
> npm install -g typescript # typescript compiler
> npm install -g tsd # typescript definition manager
% tsd install
> docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres
> docker exec -it some-postgres bash
\# createuser -U postgres -d -P -E db_user
\# createdb -U postgres --owner db_user --encoding utf8 tododb
> ./node_modules/db-migrate/bin/db-migrate up
[INFO] Processed migration 20160119093012-create-todos
[INFO] Processed migration 20160119093812-add-order-to-todos
[INFO] Done
> tsc
> set -x DATABASE_URL postgres://db_user:db_pass@localhost/tododb
> node app/server.js
% DATABASE_URL=postgres://db_user:db_pass@localhost/tododb node server.js
I use httpie utility to test REST API calls from the terminal. You can install it on OSX with brew.
> brew install httpie
You can also use the built-in curl
command if you don't want to install httpie
.
> curl -d '{"title": "Deploy on Heroku", "order": 1}' -H "Content-Type: application/json" -X POST http://localhost:5050
> http post :5050 title='Deploy on Heroku' order=1
HTTP/1.1 200 OK
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: GET,POST,PATCH,DELETE
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 88
Content-Type: application/json; charset=utf-8
Date: Tue, 26 Jan 2016 01:13:48 GMT
ETag: W/"58-6XGc4CPnaa+/LbxwvorWgw"
X-Powered-By: Express
{
"completed": false,
"order": 1,
"title": "Deploy on Heroku",
"url": "http://localhost:5000/1"
}
> curl -v -X GET localhost:5050/
> http :5050
HTTP/1.1 200 OK
Access-Control-Allow-Headers: Content-Type
Access-Control-Allow-Methods: GET,POST,PATCH,DELETE
Access-Control-Allow-Origin: *
Connection: keep-alive
Content-Length: 90
Content-Type: application/json; charset=utf-8
Date: Tue, 26 Jan 2016 01:14:14 GMT
ETag: W/"5a-I5kxfNHhoZBy7GPZa6prYg"
X-Powered-By: Express
[
{
"completed": false,
"order": 1,
"title": "Deploy on Heroku",
"url": "http://localhost:5000/1"
}
]