Skip to content

Commit f31160c

Browse files
committed
feat(cli): use app.restServer.url for console logs
Rework and simplify the application project template: - Remove console logs from `Application.prototype.start()`, let the caller print any logs if they wish to. - Modify the `main` function to log the URL to the console, use `app.restServer.url` to obtain the URL where the app is listening on. - Update example projects to follow this new style.
1 parent 5b83a0c commit f31160c

File tree

7 files changed

+11
-24
lines changed

7 files changed

+11
-24
lines changed

examples/rpc-server/src/application.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,4 @@ export class MyApplication extends Application {
1818
this.options.port = this.options.port || 3000;
1919
this.bind('rpcServer.config').to(this.options);
2020
}
21-
22-
async start() {
23-
await super.start();
24-
console.log(`Server is running on port ${this.options.port}`);
25-
}
2621
}

examples/rpc-server/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export async function main(options?: ApplicationConfig) {
1010
const app = new MyApplication(options);
1111

1212
await app.start();
13+
console.log(`Server is running on port ${app.options.port}`);
1314
return app;
1415
}
1516

examples/todo-list/src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55

66
import {TodoListApplication} from './application';
77
import {ApplicationConfig} from '@loopback/core';
8-
import {RestServer} from '@loopback/rest';
98

109
export async function main(options?: ApplicationConfig) {
1110
const app = new TodoListApplication(options);
1211
await app.boot();
1312
await app.start();
1413

15-
const server = await app.getServer(RestServer);
16-
const port = await server.get<number>('rest.port');
17-
console.log(`Server is running at http://127.0.0.1:${port}`);
14+
const url = app.restServer.url;
15+
console.log(`Server is running at ${url}`);
1816
return app;
1917
}

examples/todo/src/index.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,13 @@
55

66
import {TodoListApplication} from './application';
77
import {ApplicationConfig} from '@loopback/core';
8-
import {RestServer} from '@loopback/rest';
98

109
export async function main(options?: ApplicationConfig) {
1110
const app = new TodoListApplication(options);
1211
await app.boot();
1312
await app.start();
1413

15-
const server = await app.getServer(RestServer);
16-
const port = await server.get<number>('rest.port');
17-
console.log(`Server is running at http://127.0.0.1:${port}`);
14+
const url = app.restServer.url;
15+
console.log(`Server is running at ${url}`);
1816
return app;
1917
}

packages/cli/generators/app/templates/src/application.ts.ejs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {ApplicationConfig} from '@loopback/core';
2-
import {RestApplication, RestServer, RestBindings} from '@loopback/rest';
2+
import {RestApplication} from '@loopback/rest';
33
import {MySequence} from './sequence';
44

55
/* tslint:disable:no-unused-variable */
@@ -25,13 +25,4 @@ export class <%= project.applicationName %> extends BootMixin(RestApplication) {
2525
},
2626
};
2727
}
28-
29-
async start() {
30-
await super.start();
31-
32-
const server = await this.getServer(RestServer);
33-
const port = await server.get(RestBindings.PORT);
34-
console.log(`Server is running at http://127.0.0.1:${port}`);
35-
console.log(`Try http://127.0.0.1:${port}/ping`);
36-
}
3728
}

packages/cli/generators/app/templates/src/index.ts.ejs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,10 @@ export async function main(options?: ApplicationConfig) {
77
const app = new <%= project.applicationName %>(options);
88
await app.boot();
99
await app.start();
10+
11+
const url = app.restServer.url;
12+
console.log(`Server is running at ${url}`);
13+
console.log(`Try ${url}/ping`);
14+
1015
return app;
1116
}

packages/cli/test/integration/generators/app.integration.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ describe('app-generator specific files', () => {
3434
/class MyAppApplication extends BootMixin\(RestApplication/,
3535
);
3636
assert.fileContent('src/application.ts', /constructor\(/);
37-
assert.fileContent('src/application.ts', /async start\(/);
3837
assert.fileContent('src/application.ts', /this.projectRoot = __dirname/);
3938

4039
assert.file('src/index.ts');

0 commit comments

Comments
 (0)