Wednesday, January 1, 2020

Getting Started (Loopback 3)

Install

Assuming you have already installed Node...
Install the LoopBack CLI tool.
$ npm install -g loopback-cli

Create app

Create a "Hello World" LoopBack application.
$ lb
? What's the name of your application? hello-world
? Enter name of the directory to contain the project: hello-world

? Which version of LoopBack would you like to use? 3.x (Active Long Term Support)
? What kind of application do you have in mind? hello-world (A project containing a controller,
including a single vanilla Message and a single remote method)
...
I'm all done. Running npm install for you to install the required dependencies.
If this fails, try running the command yourself.
... 

Create models

$ lb model

The generator guides you through creating your model. Enter the values highlighted in green. To accept the default, just press Enter.
[?] Enter the model name: person
[?] Select the data-source to attach person to: db (memory)
[?] Select model`s base class (PersistedModel)
[?] Expose person via the REST API? Yes
[?] Custom plural form (used to build REST URL): people
[?] Common model or server only? common
Let's add some person properties now.

Define a firstname property for the person model
Enter an empty property name when done.
[?] Property name: firstname

Hit Enter to accept the default string type:
[?] Property type: (Use arrow keys)
❯ string
  number
  boolean
  object
  array
  date
  buffer
  geopoint
  any
  (other)

Make the property required.
[?] Required? (y/N) y

Repeat these steps for lastname property.
Press Enter when prompted for a property name to finish up and create the model.

Run the application

Run as you would any Node application.
$ node .
Browse your REST API at http://0.0.0.0:3000/explorer
Web server listening at: http://0.0.0.0:3000/

When you're ready to think about moving to production, give IBM API Connect a try. It enables you to easily deploy your LoopBack app to the cloud or on-premises. It has a built-in API gateway, and it has a visual editor for LoopBack apps!

Explore your REST API

Load http://0.0.0.0:3000/explorer to see the built-in API Explorer.
The API Explorer enables you to exercise all the generated API endpoints. There are create, read, update, and delete (CRUD) endpoints for the people model you just created.
For more information, see the Using the API Explorer in the documentation. You may also want to follow the Getting Started tutorial.

No comments:

Post a Comment