Skip to content

Latest commit

 

History

History
137 lines (102 loc) · 3.03 KB

README.md

File metadata and controls

137 lines (102 loc) · 3.03 KB

todo-backend-typescript

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.

1. Install dependencies

> npm install
> npm install -g typescript # typescript compiler
> npm install -g tsd # typescript definition manager

Install type definitions

% tsd install

2. Set up postgres db (OSX)

Run postgres via docker

> docker run --name some-postgres -e POSTGRES_PASSWORD=mysecretpassword -d postgres

Create user and database

> docker exec -it some-postgres bash
\# createuser -U postgres -d -P -E db_user
\# createdb -U postgres --owner db_user --encoding utf8 tododb

Create table

> ./node_modules/db-migrate/bin/db-migrate up
[INFO] Processed migration 20160119093012-create-todos
[INFO] Processed migration 20160119093812-add-order-to-todos
[INFO] Done

3. Compile TypeScript

> tsc

4. Run the server

fish shell

> set -x DATABASE_URL postgres://db_user:db_pass@localhost/tododb
> node app/server.js

bash/zsh

% DATABASE_URL=postgres://db_user:db_pass@localhost/tododb node server.js

5. Test the server

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.

Test 'create todo' api

> 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"
}

Test 'get all todos' api

> 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"
    }
]