From e9643fbd917df0aa40a5b00495cbac0f5ef49a77 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 29 Jan 2018 14:29:46 +0100 Subject: [PATCH 1/2] docs: add example-hello-world --- CODEOWNERS | 1 + packages/cli/generators/example/index.js | 4 +- packages/example-hello-world/.npmrc | 1 + packages/example-hello-world/LICENSE | 25 +++++++++++ packages/example-hello-world/README.md | 45 +++++++++++++++++++ packages/example-hello-world/index.d.ts | 6 +++ packages/example-hello-world/index.js | 14 ++++++ packages/example-hello-world/index.ts | 8 ++++ packages/example-hello-world/package.json | 41 +++++++++++++++++ .../example-hello-world/src/application.ts | 23 ++++++++++ packages/example-hello-world/src/index.ts | 16 +++++++ .../example-hello-world/tsconfig.build.json | 8 ++++ 12 files changed, 190 insertions(+), 2 deletions(-) create mode 100644 packages/example-hello-world/.npmrc create mode 100644 packages/example-hello-world/LICENSE create mode 100644 packages/example-hello-world/README.md create mode 100644 packages/example-hello-world/index.d.ts create mode 100644 packages/example-hello-world/index.js create mode 100644 packages/example-hello-world/index.ts create mode 100644 packages/example-hello-world/package.json create mode 100644 packages/example-hello-world/src/application.ts create mode 100644 packages/example-hello-world/src/index.ts create mode 100644 packages/example-hello-world/tsconfig.build.json diff --git a/CODEOWNERS b/CODEOWNERS index b882bb5a4b1d..e8fb812a9df1 100644 --- a/CODEOWNERS +++ b/CODEOWNERS @@ -10,6 +10,7 @@ packages/cli/* @raymondfeng @kjdelisle @shimks packages/context/* @bajtos @raymondfeng @kjdelisle packages/core/* @bajtos @raymondfeng @kjdelisle packages/example-getting-started/* @bajtos @kjdelisle +packages/example-hello-world/* @b-admike packages/example-log-extension/* @virkt25 packages/example-rpc-server/* @kjdelisle packages/metadata/* @raymondfeng diff --git a/packages/cli/generators/example/index.js b/packages/cli/generators/example/index.js index 8faa04539685..bf96bd6c1c67 100644 --- a/packages/cli/generators/example/index.js +++ b/packages/cli/generators/example/index.js @@ -14,8 +14,8 @@ const utils = require('../../lib/utils'); const EXAMPLES = { 'getting-started': 'An application and tutorial on how to build with LoopBack 4.', - 'log-extension': - 'An example extension project for LoopBack 4', + 'hello-world': 'A simple hello-world Application using LoopBack 4', + 'log-extension': 'An example extension project for LoopBack 4', 'rpc-server': 'A basic RPC server using a made-up protocol.', }; Object.freeze(EXAMPLES); diff --git a/packages/example-hello-world/.npmrc b/packages/example-hello-world/.npmrc new file mode 100644 index 000000000000..43c97e719a5a --- /dev/null +++ b/packages/example-hello-world/.npmrc @@ -0,0 +1 @@ +package-lock=false diff --git a/packages/example-hello-world/LICENSE b/packages/example-hello-world/LICENSE new file mode 100644 index 000000000000..1038ccf44384 --- /dev/null +++ b/packages/example-hello-world/LICENSE @@ -0,0 +1,25 @@ +Copyright (c) IBM Corp. 2018. All Rights Reserved. +Node module: @loopback/example-hello-world +This project is licensed under the MIT License, full text below. + +-------- + +MIT license + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. diff --git a/packages/example-hello-world/README.md b/packages/example-hello-world/README.md new file mode 100644 index 000000000000..c012d62810dd --- /dev/null +++ b/packages/example-hello-world/README.md @@ -0,0 +1,45 @@ +# @loopback/example-hello-world + +## Summary + +A simple hello-world application using LoopBack 4! + +## Prerequisites + +Before we can begin, you'll need to make sure you have some things installed: +- [Node.js](https://nodejs.org/en/) at v6.x or greater + +Additionally, this tutorial assumes that you are comfortable with +certain technologies, languages and concepts. +- JavaScript (ES6) +- [npm](https://www.npmjs.com/) +- [REST](https://en.wikipedia.org/wiki/Representational_state_transfer) + +## Installation + +1. Install the new loopback CLI toolkit. +``` +npm i -g @loopback/cli +``` + +2. Download the "hello-world" application. +``` +lb4 example hello-world +``` + +3. Switch to the directory and install dependencies. +``` +cd loopback-example-hello-world && npm i +``` + +## Use + +Start the app: + +``` +npm start +``` + +The application will start on port `3000`. Use your favourite browser or REST +client to access any path with a GET request, and watch it return `Hello world!`. + diff --git a/packages/example-hello-world/index.d.ts b/packages/example-hello-world/index.d.ts new file mode 100644 index 000000000000..5b23fa367144 --- /dev/null +++ b/packages/example-hello-world/index.d.ts @@ -0,0 +1,6 @@ +// Copyright IBM Corp. 2018. All Rights Reserved. +// Node module: @loopback/example-hello-world +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +export * from './dist'; diff --git a/packages/example-hello-world/index.js b/packages/example-hello-world/index.js new file mode 100644 index 000000000000..7cef61480f34 --- /dev/null +++ b/packages/example-hello-world/index.js @@ -0,0 +1,14 @@ +// Copyright IBM Corp. 2018. All Rights Reserved. +// Node module: @loopback/example-hello-world +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +const nodeMajorVersion = +process.versions.node.split('.')[0]; +const dist = nodeMajorVersion >= 7 ? './dist' : './dist6'; + +const application = (module.exports = require(dist)); + +if (require.main === module) { + // Run the application + application.main(); +} diff --git a/packages/example-hello-world/index.ts b/packages/example-hello-world/index.ts new file mode 100644 index 000000000000..170e1a96d2a7 --- /dev/null +++ b/packages/example-hello-world/index.ts @@ -0,0 +1,8 @@ +// Copyright IBM Corp. 2018. All Rights Reserved. +// Node module: @loopback/example-hello-world +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +// DO NOT EDIT THIS FILE +// Add any aditional (re)exports to src/index.ts instead. +export * from './src'; diff --git a/packages/example-hello-world/package.json b/packages/example-hello-world/package.json new file mode 100644 index 000000000000..20f814f41bc5 --- /dev/null +++ b/packages/example-hello-world/package.json @@ -0,0 +1,41 @@ +{ + "name": "@loopback/example-hello-world", + "version": "1.0.1-alpha.0", + "description": "A simple hello-world Application using LoopBack 4", + "private": true, + "main": "index.js", + "engines": { + "node": ">=6" + }, + "scripts": { + "acceptance": "lb-mocha \"DIST/test/acceptance/**/*.js\"", + "build": "npm run build:dist && npm run build:dist6", + "build:current": "lb-tsc", + "build:dist": "lb-tsc es2017", + "build:dist6": "lb-tsc es2015", + "build:apidocs": "lb-apidocs", + "clean": "lb-clean *example-hello-world*.tgz dist dist6 package api-docs", + "prepare": "npm run build && npm run build:apidocs", + "verify": "npm pack && tar xf *example-hello-world*.tgz && tree package && npm run clean", + "start": "npm run build && node ." + }, + "repository": { + "type": "git", + "url": "https://github.com/strongloop/loopback-next.git" + }, + "license": "MIT", + "dependencies": { + "@loopback/core": "^4.0.0-alpha.29", + "@loopback/rest": "^4.0.0-alpha.21" + }, + "devDependencies": { + "@loopback/build": "^4.0.0-alpha.10", + "@types/node": "^8.5.9" + }, + "keywords": [ + "loopback", + "LoopBack", + "example", + "tutorial" + ] +} diff --git a/packages/example-hello-world/src/application.ts b/packages/example-hello-world/src/application.ts new file mode 100644 index 000000000000..335794edcb7f --- /dev/null +++ b/packages/example-hello-world/src/application.ts @@ -0,0 +1,23 @@ +// Copyright IBM Corp. 2018. All Rights Reserved. +// Node module: @loopback/example-hello-world +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +import {Application} from '@loopback/core'; +import {RestApplication, RestServer} from '@loopback/rest'; + +export class HelloWorldApplication extends RestApplication { + constructor() { + super(); + this.handler((sequence, request, response) => { + sequence.send(response, 'Hello World!'); + }); + } + + async start() { + await super.start(); + + const rest = await this.getServer(RestServer); + console.log(`REST server running on port: ${await rest.get('rest.port')}`); + } +} diff --git a/packages/example-hello-world/src/index.ts b/packages/example-hello-world/src/index.ts new file mode 100644 index 000000000000..980318e29153 --- /dev/null +++ b/packages/example-hello-world/src/index.ts @@ -0,0 +1,16 @@ +// Copyright IBM Corp. 2018. All Rights Reserved. +// Node module: @loopback/example-hello-world +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +import {HelloWorldApplication} from './application'; + +export async function main() { + const app = new HelloWorldApplication(); + try { + await app.start(); + } catch (err) { + console.error(`Unable to start application: ${err}`); + } + return app; +} diff --git a/packages/example-hello-world/tsconfig.build.json b/packages/example-hello-world/tsconfig.build.json new file mode 100644 index 000000000000..1c3a54249348 --- /dev/null +++ b/packages/example-hello-world/tsconfig.build.json @@ -0,0 +1,8 @@ +{ + "$schema": "http://json.schemastore.org/tsconfig", + "extends": "./node_modules/@loopback/build/config/tsconfig.common.json", + "compilerOptions": { + "rootDir": "." + }, + "include": ["index.ts", "src"] +} From 6bfcfc22b9b3545b956fb33bf6a0c60b732dc04f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Tue, 30 Jan 2018 10:56:18 +0100 Subject: [PATCH 2/2] fixup! address review comments --- packages/example-hello-world/README.md | 6 +++++- packages/example-hello-world/package.json | 4 ++-- packages/example-hello-world/src/application.ts | 5 +++++ 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/packages/example-hello-world/README.md b/packages/example-hello-world/README.md index c012d62810dd..e19ce4f97acb 100644 --- a/packages/example-hello-world/README.md +++ b/packages/example-hello-world/README.md @@ -1,8 +1,12 @@ # @loopback/example-hello-world +A simple hello-world application using LoopBack 4! + ## Summary -A simple hello-world application using LoopBack 4! +This project shows how to write the simplest LoopBack 4 application possible. +Check out [src/application.ts](src/application.ts) to learn how we configured +our application to always respond with "Hello World!". ## Prerequisites diff --git a/packages/example-hello-world/package.json b/packages/example-hello-world/package.json index 20f814f41bc5..6a6d35a751cd 100644 --- a/packages/example-hello-world/package.json +++ b/packages/example-hello-world/package.json @@ -1,6 +1,6 @@ { "name": "@loopback/example-hello-world", - "version": "1.0.1-alpha.0", + "version": "4.0.0-alpha.0", "description": "A simple hello-world Application using LoopBack 4", "private": true, "main": "index.js", @@ -15,7 +15,7 @@ "build:dist6": "lb-tsc es2015", "build:apidocs": "lb-apidocs", "clean": "lb-clean *example-hello-world*.tgz dist dist6 package api-docs", - "prepare": "npm run build && npm run build:apidocs", + "prepublishOnly": "npm run build && npm run build:apidocs", "verify": "npm pack && tar xf *example-hello-world*.tgz && tree package && npm run clean", "start": "npm run build && node ." }, diff --git a/packages/example-hello-world/src/application.ts b/packages/example-hello-world/src/application.ts index 335794edcb7f..090e046d8be4 100644 --- a/packages/example-hello-world/src/application.ts +++ b/packages/example-hello-world/src/application.ts @@ -9,6 +9,11 @@ import {RestApplication, RestServer} from '@loopback/rest'; export class HelloWorldApplication extends RestApplication { constructor() { super(); + + // In this example project, we configure a sequence that always + // returns the same HTTP response: Hello World! + // Learn more about the concept of Sequence in our docs: + // http://loopback.io/doc/en/lb4/Sequence.html this.handler((sequence, request, response) => { sequence.send(response, 'Hello World!'); });