Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

blog pt3 #24

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion blog/00-getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ In this episode, we will install our prerequisites, get an understanding of what

**_LoopBack is so awesome, it is almost magical. Let me show you by building an API in 73 seconds:_**

[![Rapid APIs in LoopBack](https://strongloop.com/blog-assets/2017/band-app/rapid-apis-in-lb-video.png)](https://youtu.be/iOMD27DjuO4 "Rapid APIs in LoopBack")
[![Rapid APIs in LoopBack](../workshop/assets/video.png)](https://youtu.be/iOMD27DjuO4 "Rapid APIs in LoopBack")

## Let's initialize our LoopBack application

Expand Down
111 changes: 111 additions & 0 deletions blog/02-exploring-our-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Let's Build a Band App with LoopBack: Exploring Our API (part 3 of many)

In this series, we are working through building an application to support the needs of DIY bands. We'll start out solving some basic problems and move into more complex ground, eventually transforming the application into a platform that others can use and build upon.

Accompanying this series is a [corresponding workshop](https://github.com/StrongLoop-Evangelists/workshop-band-app) as well as a [code repository](https://github.com/StrongLoop-Evangelists/band-app).

## Previously on _Let's build a band app!_

In the [previous episode](https://strongloop.com/strongblog/lets-build-a-band-app-loopback-pt2/), we created our first model-driven API endpoint. We used the LoopBack CLI, which asked us a series of questions about our data and then built out our RESTful API endpoints based on the answers we provided. We took a moment to look at the files that were generated, but now we will actually see those endpoints in action.


## In this episode

Exploring our API

We've initialized a LoopBack application and built our first model-driven RESTful API endpoint for `events`. Let's run the application using `node .` and take a look at the API Explorer UI.


### Start our application

```
➜ band-app git:(master) ✗ node .
Web server listening at: http://0.0.0.0:3000
Browse your REST API at http://0.0.0.0:3000/explorer
```

As we can see above, we have started our node.js application -- upon startup, it helpfully tells us what URL our application is using as well as the URL to the API Explorer. Let's click on the Explorer URL and engage with our endpoints.

_Pro tip: most terminal applications will launch a URL if you `cmd + click` on the text._

### API Explorer

![LoopBack API Explorer - List of endpoints](../workshop/assets/api-explorer-02.png)

The browser opens up to a page that lists our API endpoints. We'll see one for `event` and one for `User`.

The **User** endpoint is the base class that is provided by the `api server` option we chose when initializing our application. It is the class that we would extend from when making any sort of user-based role; `members`, for example.

_Pro tip: a programming convention is to name Base classes with a capital letter. Reminder: Base classes are the ones that you do not edit directly, but instead extend from._

The **event** endpoint is the one created in the previous step based on our answers to the prompts.

### Our first endpoint: `event`

![LoopBack API Explorer - product endpoint](../workshop/assets/api-explorer-03.png)

This is where the magic of LoopBack shines through for those just getting started. Based on the questions related to our model, we are given this full CRUD RESTful API. Let's take this moment to appreciate LoopBack: we haven't written any code yet. ✨MAGIC✨ (And some super-smart generator actions.)

### A `GET` request

![LoopBack API Explorer - GET request](../workshop/assets/api-explorer-04.png)

If we open up the details of the `GET` request panel in our explorer, we will see some details of our model schema, a parameter section (we'll get to later), as well as a button to "Try it out!"

### A `GET` request - Try it out!

Clicking the "Try it out!" button makes a `GET` request to our `event` endpoint:

![LoopBack API Explorer - GET request response](../workshop/assets/api-explorer-05.png)

In addition to providing some request examples (Curl and URL), we also see that the `GET` request is successful with a `200` response, but the response array is empty. That's because we don't have any data! We've only described our data, but we haven't actually added any to our data-source. Let's try adding some data using the `POST` method.

*Pro tip: To learn more about HTTP responses, check out this [list of http status codes](https://en.wikipedia.org/wiki/List_of_HTTP_status_codes). One of my personal favorites is `418 - I'm a teapot`.*

### A POST call - add some data

![LoopBack API Explorer - POST method](../workshop/assets/api-explorer-06.png)

If we scroll the page down the list of HTTP verbs, we will see a few POST options. The first one should be `POST /events`. Let's open that panel by clicking on it and we should see a similar panel to the GET one we saw above.

### A `POST` request - Try it out!

We can click on the text field with the yellow background in the Parameters section. Doing so will populate the text field (with the white background) with example data. In the image above, I edited the example data like so:

```json
{
"date": "June 9, 2017 20:00:00",
"location": "2nd Nature Skatepark",
"cost": 5,
"city": "Peekskill",
"state": "NY",
"description": "A punk rock show at a punk rock place",
"age-restriction": "All ages",
"lineup": [
"Jukebox Romantics",
"Least Best Beast",
"Kittyhawk"
]
}

```

There are a few things to notice in our data:

- it is trimmed: everything in this model is optional
- the `id` key/value in the data is removed: if we send an `id`, we may get an error because that field is managed by the data source
- the date is formatted differently than the sample data: LoopBack handles dates rather nicely

After entering this data into the text field, we can click the 'Try it out!' button to `POST` this data to the endpoint.

![LoopBack API Explorer - POST method response](../workshop/assets/api-explorer-07.png)

If the request is successful (as it should be), we get a `200` response and the response body includes the data we sent with an actual `id` returned. To verify the successful `POST`, go back to the `GET` request and "Try it out!"

![LoopBack API Explorer - GET method with data](../workshop/assets/api-explorer-08.png)

Yay! Again, we get the `200` response and this time, the `GET` request returns an array with the object we had submitted in the previous step. Exciting, right?

Now that we can see how to interact with our API endpoints through the explorer, let's hook this up to an actual datasource, something we will tackle in the next episode. Until then, we could try making these requests through our command line with cURL requests or by hitting the endpoints directly in the browser. We may even start to implement these requests in some HTML and make AJAX requests to get data. This should keep us busy for a few until next time!


176 changes: 176 additions & 0 deletions blog/04-deploy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
# Deploy

🖥 To see the code for this step, here is [commit/diff](https://github.com/StrongLoop-Evangelists/band-app/commit/b065a8854831c44371aa2c49bc9f365ffd6f2b68
).

---

A LoopBack app is simply a Node.js app. For our purposes, we will deploy our code to [Bluemix](https://bluemix.net), IBM's PaaS (platform as a service), but we could deploy it wherever Node.js is supported. If we were to deploy our app to a conventional server, we would need to make sure the tools were in place to keep our app running and able to handle any sort of load it may need to withstand. Fortunately, Bluemix handles all of that for us. And we can use their Cloud Foundry tools to configure and manage the deployment.

## Create Node.js Cloud Foundry app:

In this step, we need to go into the [Bluemix catalog](https://console.ng.bluemix.net/catalog/) and from the Cloud Foundry section we will choose to create a Node.js Runtime App:

![Bluemix - Cloud Foundry Apps](assets/bluemix-catalog-cloud-foundry-apps.png)

### Create the Node.js app

Choosing the SDK for Node.js takes us to a page where we can name our application as well as choose the Bluemix route to access our app:

![Bluemix - Create Node.js app](assets/bluemix-create-node-starter.png)

*Note: take notice of the org and space you created this app in on Bluemix. It is likely you only have one of each, but it is not uncommon to have more than one of one or the other. Which org/space you are currently in is where the app will be created.*

The next page presented to us after creating our Node.js app shows the app starting up. On this page, we will also see some instructions for using a sample app and getting started. We are going to skip that in favor of the following steps:

## Prepare our app and local environment

We can now switch back to the terminal and our editor to get our local environment prepped and our application set up.

### Create our `manifest.yml` file

Let's go into the root of our LoopBack application and create a `manifest.yml` file and add the following information, changing "name" to the name we chose when creating the Cloud Foundry app.

It should look something like so:

```yaml
name: Band-App
memory: 256M
```

### Get Cloud Foundry

At this point, we are going to begin using the Cloud Foundry cli, so we'll need to get that installed before going further.

The most common way of installing Cloud Foundry on a Mac is to use Homebrew:

```bash
$ brew tap cloudfoundry/tap
$ brew install cf-cli
```

To find instructions for other platforms, see [Cloud Foundry CLI downloads page](https://github.com/cloudfoundry/cli#downloads)

### Login `cf login`

The first thing we need to do is log in. Doing so, it will prompt us for email, password, organization and space.

In the terminal, let's run `cf login`:

```bash
➜ band-app git:(master) ✗ cf login
API endpoint: https://api.ng.bluemix.net

Email> [email protected]

Password>
Authenticating...
OK

Select an org (or press enter to skip):
1. StrongLoop Evangelists Org
2. Some Total

Org> 1
Targeted org StrongLoop Evangelists Org

Select a space (or press enter to skip):
1. dev
2. acme-freight
3. Band App

Space> 3
Targeted space Band App


API endpoint: https://api.ng.bluemix.net (API version: 2.54.0)
User: [email protected]
Org: StrongLoop Evangelists Org
Space: Band App
```

Looks like we were successful.

## Deploy!

### Push our code `cf push`

We can push our code to Bluemix by running the `cf push` command. Doing so will use our `manifest.yml` file and the settings we have just chosen to deploy our app in the cloud.

```bash
➜ band-app git:(master) ✗ cf push
Using manifest file /Users/joesepi/code/band-app/manifest.yml

Updating app Band-App in org StrongLoop Evangelists Org / space Band App as [email protected]...
OK

Uploading band-app...
Uploading app files from: /Users/joesepi/code/band-app
Uploading 11.1M, 11337 files
Done uploading
OK

Stopping app band-app in org StrongLoop Evangelists Org / space Band App as [email protected]...
OK

Starting app band-app in org StrongLoop Evangelists Org / space Band App as [email protected]...
```

Beyond this, there is a lot of output about things downloading and such, but after the app is prepared and then started, we will something along these lines in your terminal:

```
Staging complete
Exit status 0
Uploading droplet, build artifacts cache...
Uploading build artifacts cache...
Uploading droplet...
Uploaded build artifacts cache (12.2M)
Uploaded droplet (29M)
Uploading complete

0 of 1 instances running, 1 starting
0 of 1 instances running, 1 starting
0 of 1 instances running, 1 starting
1 of 1 instances running

App started


OK

App band-app was started using this command `./vendor/initial_startup.rb`

Showing health and status for app band-app in org StrongLoop Evangelists Org / space Band App as [email protected]...
OK

requested state: started
instances: 1/1
usage: 256M x 1 instances
urls: Band-App.mybluemix.net
last uploaded: Tue Apr 18 03:27:35 UTC 2017
stack: cflinuxfs2
buildpack: SDK for Node.js(TM) (ibm-node.js-4.8.0, buildpack-v3.11-20170303-1144)

state since cpu memory disk details
#0 running 2017-04-17 11:29:22 PM 0.0% 123.9M of 256M 144.1M of 1G
```

At this point, our app is in the cloud and we can visit the app at the urls stated in the output above: `urls: band-app.mybluemix.net` (*your url may be slightly different*)

### Show our app status `cf apps`

And at any point, we can check our apps by running `cf apps`. The output should looks something like:

```bash
➜ band-app git:(master) ✗ cf apps
Getting apps in org StrongLoop Evangelists Org / space Band App as [email protected]...
OK

name requested state instances memory disk urls
band-app started 1/1 256M 1G Band-App.mybluemix.net
```

If we go to our [Band App on Bluemix](https://band-app.bluemix.net), we will see the status output that is the default for a LoopBack app. And if we view the [LoopBack Explorer for our API](https://band-app.bluemix.net/explorer)(`/explorer`), we should see the familiar interface.

**Next Step:** [Connect a datasource](06-datasource.md)