|
| 1 | +--- |
| 2 | +lang: en |
| 3 | +title: 'Create your first app' |
| 4 | +keywords: LoopBack 4.0, LoopBack 4, Node.js, TypeScript, OpenAPI |
| 5 | +sidebar: lb4_sidebar |
| 6 | +permalink: /doc/en/lb4/Create-your-first-app.html |
| 7 | +summary: Write and run a LoopBack 4 "Hello World" project in TypeScript. |
| 8 | +--- |
| 9 | + |
| 10 | +## Create Your First App |
| 11 | + |
| 12 | +### Create a new project |
| 13 | + |
| 14 | +The LoopBack 4 CLI scaffolds a new project by setting up the project structure, |
| 15 | +configuring the TypeScript, and installing required dependencies. |
| 16 | + |
| 17 | +To create a new project, run the following command and follow the prompts: |
| 18 | + |
| 19 | +```sh |
| 20 | +lb4 app |
| 21 | +``` |
| 22 | + |
| 23 | +Provide the following inputs when prompted: |
| 24 | + |
| 25 | +```sh |
| 26 | +? Project name: getting-started |
| 27 | +? Project description: Getting started tutorial |
| 28 | +? Project root directory: getting-started |
| 29 | +? Application class name: StarterApplication |
| 30 | +? Select features to enable in the project: |
| 31 | +❯◉ Enable eslint: add a linter with pre-configured lint rules |
| 32 | + ◉ Enable prettier: install prettier to format code conforming to rules |
| 33 | + ◉ Enable mocha: install mocha to run tests |
| 34 | + ◉ Enable loopbackBuild: use @loopback/build helpers (e.g. lb-eslint) |
| 35 | + ◉ Enable editorconfig: add EditorConfig files |
| 36 | + ◉ Enable vscode: add VSCode config files |
| 37 | + ◉ Enable docker: include Dockerfile and .dockerignore |
| 38 | + ◉ Enable repositories: include repository imports and RepositoryMixin |
| 39 | + ◉ Enable services: include service-proxy imports and ServiceMixin |
| 40 | +``` |
| 41 | + |
| 42 | +### Starting the project |
| 43 | + |
| 44 | +The generated project includes a built-in `/ping` endpoint for testing. |
| 45 | + |
| 46 | +Start the application: |
| 47 | + |
| 48 | +```sh |
| 49 | +cd getting-started |
| 50 | +npm install |
| 51 | +npm start |
| 52 | +``` |
| 53 | + |
| 54 | +Open your brower and navigate to: |
| 55 | +[http://127.0.0.1:3000/ping](http://127.0.0.1:3000/ping). |
| 56 | + |
| 57 | +### Add a Controller |
| 58 | + |
| 59 | +Next, add a custom [controller](Controller.md). In this example, you’ll create a |
| 60 | +simple “Hello World” endpoint. |
| 61 | + |
| 62 | +Run: |
| 63 | + |
| 64 | +```sh |
| 65 | +lb4 controller |
| 66 | +``` |
| 67 | + |
| 68 | +- _Note: If your application is running, press **CTRL+C** to stop it before |
| 69 | + running the command._ |
| 70 | + |
| 71 | +Respond to the prompts: |
| 72 | + |
| 73 | +```sh |
| 74 | +? Controller class name: hello |
| 75 | +? What kind of controller would you like to generate? Empty Controller |
| 76 | + create src/controllers/hello.controller.ts |
| 77 | + update src/controllers/index.ts |
| 78 | + |
| 79 | +Controller hello was now created in src/controllers/ |
| 80 | +``` |
| 81 | + |
| 82 | +### Implement the Controller |
| 83 | + |
| 84 | +Replace the contents of `src/controllers/hello.controller.ts` with the |
| 85 | +following: |
| 86 | + |
| 87 | +```ts |
| 88 | +import {get} from '@loopback/rest'; |
| 89 | + |
| 90 | +export class HelloController { |
| 91 | + @get('/hello') |
| 92 | + hello(): string { |
| 93 | + return 'Hello world!'; |
| 94 | + } |
| 95 | +} |
| 96 | +``` |
| 97 | + |
| 98 | +### Test the Endpoint |
| 99 | + |
| 100 | +Restart the application: |
| 101 | + |
| 102 | +```sh |
| 103 | +npm start |
| 104 | +``` |
| 105 | + |
| 106 | +Open your browser and navigate to: |
| 107 | +[http://127.0.0.1:3000/hello](http://127.0.0.1:3000/hello) |
| 108 | + |
| 109 | +You should see: |
| 110 | + |
| 111 | +``` |
| 112 | +Hello world! |
| 113 | +``` |
| 114 | + |
| 115 | +### Code sample |
| 116 | + |
| 117 | +View the complete example project here: |
| 118 | +[https://github.com/loopbackio/loopback-next/tree/master/examples/hello-world](https://github.com/loopbackio/loopback-next/tree/master/examples/hello-world) |
0 commit comments