Crafting Node APIs, Part 2: Getting Started

Previously: Crafting Node APIs part 1

Objective

Scott Wojan, projekt202 Principal Architect

Reggie Samuel, projekt202 Managing Architect

In this post, we will get your environment set up and start interacting with the starter project.

Quick Note

In Part 1, we discussed a REST API and a number of considerations you should keep in mind when beginning to craft an API. One of those topics was the persistence mechanism. There are a wealth of posts out there that use NoSQL implementations because, let's face it, it's just so simple to stick JSON into one and get it back out. We thought it would be much more interesting and more "real world" to implement this using a relational database, specifically MySQL.

Prerequisites

  1. Install git
  2. Install node
  3. Install MySQL and MySQL Workbench Mac users, please note that MySQL doesn't start automatically; please follow these instructions: http://dev.mysql.com/doc/refman/5.7/en/osx-installation-launchd.html
  4. Create a new database and user in MySQL called Todo

    [code lang="sql"] CREATE USER todo IDENTIFIED by 'todo'; CREATE DATABASE IF NOT EXISTS todo; GRANT ALL PRIVILEGES ON todo.* to 'todo'@'localhost' WITH GRANT OPTION; [/code]

Obtaining the Source Code

You can obtain the source code by navigating to the folder where you want the code to exist, using either terminal or the command window and issuing the following command:

[code lang="shell"] #clone repository: git clone https://github.com/projekt202/blog-node-api.git

#install required node packages: npm install

#create the database tables: node database/create.js [/code]

Diving Right in!

Starting the App

Assuming you have the terminal or command window open, and your current directory is the source code folder, you can run the app by issuing the following command:

[code lang="shell"]#Start the app: node app.js[/code]

You should receive a message indicating that the Todo Services is listening locally on port 3000.

Creating a User

You can issue the following curl commands directly on a Mac or you can import them into the highly-recommended POSTMAN tool:

[code lang="shell"]curl -X POST -H "Accept: application/json" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{ "emailAddress": "tommyt@nowhere.com", "firstName": "Tommy", "lastName": "Tester" }' "http://127.0.0.1:3000/users"[/code]

You should receive a 200 OK response with the following application/json data indicating the user was successfully created. Please note the user id that is generated and returned in the response:

[code lang="js"]{ "id": 1, "emailAddress": "tommyt@nowhere.com", "firstName": "Tommy", "lastName": "Tester", "updatedAt": "2016-09-13T17:02:33.000Z", "createdAt": "2016-09-13T17:02:33.000Z" }[/code]

Getting a User's Information

We will issue another http request, this time a GET with the user's ID.

[code lang="shell"]curl -X GET -H "Accept: application/json" -H "Content-Type: application/json" -H "Cache-Control: no-cache" "http://127.0.0.1:3000/users/1"[/code]

You should again receive a 200 OK response with the following application/json data containing the user's info:

[code lang="js"]{ "id": 1, "emailAddress": "tommyt@nowhere.com", "firstName": "Tommy", "lastName": "Tester", "updatedAt": "2016-09-13T17:02:33.000Z", "createdAt": "2016-09-13T17:02:33.000Z" }[/code]

Updating a User

[code lang="shell"]curl -X PUT -H "Accept: application/json" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -H "Postman-Token: 6bfc1f25-6317-e3bc-aa30-3435bc9e3ea4" -d '{ "id": 1, "emailAddress": "tommyt@nowhere.com", "firstName": "Thomas", "lastName": "Tester" }' "http://127.0.0.1:3000/users/1"[/code]

You should receive a 200 OK response with the following application/json data indicating the user was successfully created:

[code lang="js"]{ "id": 2, "emailAddress": "tommyt@nowhere.com", "firstName": "Thomas", "lastName": "Tester", "createdAt": "2016-09-13T17:26:23.000Z", "updatedAt": "2016-09-13T18:40:52.000Z" }[/code]

Creating a User's Todo Items

We will submit a couple of todo items for a user.

[code lang="shell"]curl -X POST -H "Accept: application/json" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{ "userId": 1, "name": "Get Groceries", "content": "Get butter and milk" }' "http://127.0.0.1:3000/users/2/todos" [/code]

You should receive a 200 OK response with the following application/json data indicating the todo was successfully created:

[code lang="js"]{ "id": 1, "userId": 1, "name": "Get Groceries", "content": "Get butter and milk", "updatedAt": "2016-09-13T18:03:28.000Z", "createdAt": "2016-09-13T18:03:28.000Z" }[/code]

And another one:

[code lang="shell"]curl -X POST -H "Accept: application/json" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{ "userId": 1, "name": "Get oil change", "content": "Grab coupon off of desk" }' "http://127.0.0.1:3000/users/2/todos" [/code]

You should receive a 200 OK response with the following application/json data indicating the todo was successfully created:

[code lang="js"]{ "id": 2, "userId": 1, "name": "Get oil change", "content": "Grab coupon off of desk", "updatedAt": "2016-09-13T18:12:06.000Z", "createdAt": "2016-09-13T18:12:06.000Z" }[/code]

Getting a User's Todo Items

[code lang="shell"]curl -X GET -H "Accept: application/json" -H "Content-Type: application/json" -H "Cache-Control: no-cache" "http://127.0.0.1:3000/users/1/todos"[/code]

You should again receive a 200 OK response with the following application/json data containing the user's todo items:

[code lang="js"][ { "id": 1, "name": "Get Groceries", "content": "Get butter and milk", "createdAt": "2016-09-13T18:03:28.000Z", "updatedAt": "2016-09-13T18:03:28.000Z", "userId": 2 }, { "id": 2, "name": "Get oil change", "content": "Grab coupon off of desk", "createdAt": "2016-09-13T18:12:06.000Z", "updatedAt": "2016-09-13T18:12:06.000Z", "userId": 2 } ][/code]

Deleting a Todo Item

[code lang="shell"]curl -X DELETE -H "Accept: application/json" -H "Content-Type: application/json" -H "Cache-Control: no-cache" "http://127.0.0.1:3000/users/2/todos/1"[/code]

You should receive a 200 OK response indicating the todo was deleted.

Conclusion

In this post, we have configured your environment, downloaded the source code and started interacting with our API. In Part 3, we will start walking through how the API was constructed.


Stay up-to-date on projekt202 news by following us on LinkedIn, Twitter and Facebook.