Use the application generator tool to quickly create a LoopBack application, models, and data sources.

Page Contents

Create new application

To create a new application, run the LoopBack application generator.

If using loopback-cli:

$ lb

If using apic:

$ apic loopback

If using slc:

$ slc loopback

The LoopBack application generator will greet you with some friendly ASCII art and prompt you for the name of the application. (For apic or slc it may show slightly differently, but still request for the application name.)

Enter loopback-getting-started. Then the generator will prompt you for the name of the directory to contain the project; press Enter to accept the default (the same as the application name):

     _-----_
    |       |    .--------------------------.
    |--(o)--|    |  Let's create a LoopBack |
   `---------´   |       application!       |
    ( _´U`_ )    '--------------------------'
    /___A___\
     |  ~  |
   __'.___.'__
 ´   `  |° ´ Y `
[?] What's the name of your application? loopback-getting-started
[?] Enter name of the directory to contain the project: loopback-getting-started

Then the tool will ask you what kind of application to create:

If using apic:

? What kind of application do you have in mind? (Use arrow keys)
❯ empty-server (An empty LoopBack API, without any configured models or datasources)
  hello-world (A project containing a controller, including a single vanilla Message and
    a single remote method)
  notes (A project containing a basic working example, including a memory database)

Press Enter to accept the default selection, empty server.

If using slc:

? What kind of application do you have in mind? (Use arrow keys)
  api-server (A LoopBack API server with local User auth)
  empty-server (An empty LoopBack API, without any configured models or datasources)
❯ hello-world (A project containing a controller, including a single vanilla Message and
    a single remote method)
  notes (A project containing a basic working example, including a memory database)

Arrow down and choose hello-world.

The generator will then display messages as it scaffolds the application including:

  1. Initializing the project folder structure.
  2. Creating default JSON files.
  3. Creating default JavaScript files.
  4. Downloading and installing dependent Node modules (as if you had manually done npm install).

Create models

Now that you’ve scaffolded the initial project, you’re going to create a CoffeeShop model that will automatically have REST API endpoints.

Go into your new application directory, then run the LoopBack model generator:

$ cd loopback-getting-started

Then, using IBM API Connect v5 developer toolkit:

$ apic create --type model

Using StrongLoop tools:

$ lb model

The generator will prompt for a model name.  Enter CoffeeShop:

[?] Enter the model name: CoffeeShop

It will ask if you want to attach the model to any data sources that have already been defined.  

At this point, only the default in-memory data source is available.  Press Enter to select it:

...
[?] Select the data-source to attach CoffeeShop to: (Use arrow keys)
❯ db (memory)

Then the generator will prompt you for the base class to use for the model.  Since you will eventually connect this model to a persistent data source in a database, press down-arrow to choose PersistedModel, then press Enter:

[?] Select model's base class: (Use arrow keys)
  Model
❯ PersistedModel
  ACL
  AccessToken
  Application
  Change
  Checkpoint

PersistedModel is the base object for all models connected to a persistent data source such as a database.  See LoopBack core concepts for an overview of the model inheritance hierarchy.

One of the powerful advantages of LoopBack is that it automatically generates a REST API for your model.  The generator will ask whether you want to expose this REST API.

Hit Enter again to accept the default and expose the CoffeeShop model via REST:

[?] Expose CoffeeShop via the REST API? (Y/n) Y

LoopBack automatically creates a REST route associated with your model using the plural of the model name.  By default, it pluralizes the name for you (by adding “s”), but you can specify a custom plural form if you wish.  See Exposing models over REST for all the details.  

Press Enter to accept the default plural form (CoffeeShops):

[?] Custom plural form (used to build REST URL):

Next, you’ll be asked whether you want to create the model on the server only or in the /common directory, where it can potentially be used by both server and client LoopBack APIs.  Keep, the default, common, even though in this application you’ll only be working with server-side models:

? Common model or server only?
❯ common
  server

Every model has properties.  Right now, you’re going to define one property, “name,” for the CoffeeShop model.  

Select string as the property type (press Enter, since string is the default choice):

Let's add some CoffeeShop properties now.
Enter an empty property name when done.
[?] Property name: name
   invoke   loopback:property
[?] Property type: (Use arrow keys)
❯ string
  number
  boolean
  object
  array
  date
  buffer
  geopoint
  (other)

Each property can be optional or required. Enter y to make name required:

[?] Required? (y/N)

Then you’ll be prompted to enter a default value for the property; press Enter for no default value:

? Default value[leave blank for none]:

Then, you’ll be prompted to add another property.  Follow the prompts to add a required property named “city.”

Let's add another CoffeeShop property.
? Property name: city
? Property type: string
? Required? Yes
? Default value[leave blank for none]:

End the model creation process by pressing Enter when prompted for the name of the next property.

The model generator will create two files in the application’s common/models directory that define the model: coffee-shop.json and coffee-shop.js.

Check out the project structure

For all the details of the canonical LoopBack application structure, see Project layout reference.

Run the application

Start the application:

$ node .
...
Browse your REST API at http://0.0.0.0:3000/explorer
Web server listening at: http://0.0.0.0:3000/

Open your browser to http://0.0.0.0:3000/ (on some systems, you may need to use http://localhost:3000 instead).  You’ll see the default application response that displays some JSON with some status information; for example:

{"started":"2016-09-10T21:59:47.155Z","uptime":42.054}

Now open your browser to http://0.0.0.0:3000/explorer or http://localhost:3000/explorer.  You’ll see the StrongLoop API Explorer:

</figure>

Through a set of simple steps using LoopBack, you’ve created a CoffeeShop model, specified its properties and then exposed it through REST. 

Next: In Use API Explorer, you’ll explore the REST API you just created in more depth and exercise some of its operations.