Redirecting to http://loopback.io/doc/en/lb2/Create-a-simple-API.html ...
Use the LoopBack command-line tool,
slc loopback, to create and scaffold applications. Scaffolding simply means generating the basic code for your application to save you time. You can then extend and modify the code as desired for your specific needs.
Create new application
To create a new application, run the LoopBack application generator:
The LoopBack generator will greet you with some friendly ASCII art and prompt you for the name of the application.
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):
The generator will scaffold the application including:
- Initializing the project folder structure.
- Creating default JSON files.
- Downloading and installing dependent Node modules (as if you had manually done
Now that you've scaffolded the initial project, you're going to create create a CoffeeShop model that will automatically have REST API endpoints.
Go into your new application directory, then run the LoopBack model generator:
The generator will prompt for a model name. Enter 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:
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:
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 Person model via REST:
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):
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:
Every model has properties. Right now, you're going to define one property, "name," for the CoffeeShop model.
string as the property type (press Enter, since string is the default choice):
Each property can be optional or required. Enter
y to make
Then you'll be prompted to enter a default value for the property; press Enter for no default value:
Then, you'll be prompted to add another property. Follow the prompts to add a required property named "city."
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:
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:
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:
Through a set of simple steps using LoopBack, you've created a CoffeeShop model, specified its properties and then exposed it through REST.