Before you install LoopBack, make sure to download and install Node.js (version 8.9.x or higher), a JavaScript runtime.
The LoopBack 4 CLI is a command-line interface that can scaffold a project or extension. The CLI provides the fastest way to get started with a LoopBack 4 project that adheres to best practices.
$ npm install -g @loopback/cli
To create a new project, run the CLI as follows.
$ lb4 app
Answer the prompts as follows.
[?] Project name: getting-started[?] Project description: Getting started tutorial[?] Project root directory: (getting-started)[?] Application class name: StarterApplication[?] Select features to enable in the project: Enable eslint, Enable prettier, Enable mocha, Enable loopbackBuild, Enable vscode, Enable docker, Enable repositories, Enable services
The project comes with a "ping" route to test the project. Let's try it out by running the projects.
In a browser, visit http://127.0.0.1:3000/ping
$ cd getting-started$ npm start
Now that we have a basic project created, it’s time to add our own controller. Let’s add a simple “Hello World” controller as follows.
$ lb4 controller[?] Controller class name: hello[?] What kind of controller would you like to generate? Empty Controller create src/controllers/hello.controller.ts update src/controllers/index.ts Controller Hello was now created in src/controllers/
Paste the following contents into the file /src/controllers/hello.controller.ts
.
import {get }from '@loopback/rest';export class HelloController { @get('/hello') hello(): string {return 'Hello world!'; } }
Start the application using npm start
.
Hello world!
Follow the tutorial to create a real-world application with LoopBack 4.