1
This commit is contained in:
parent
1c6c3d38fb
commit
4f8a631ae2
1110
CHANGELOG.md
Normal file
1110
CHANGELOG.md
Normal file
File diff suppressed because it is too large
Load Diff
25
LICENSE
Normal file
25
LICENSE
Normal file
@ -0,0 +1,25 @@
|
||||
Copyright (c) IBM Corp. and LoopBack contributors 2018,2019.
|
||||
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.
|
||||
75
README.md
75
README.md
@ -1 +1,74 @@
|
||||
# testnode
|
||||
# @loopback/example-hello-world
|
||||
|
||||
A simple hello-world application using LoopBack 4!
|
||||
|
||||
## Summary
|
||||
|
||||
This project shows how to write the simplest LoopBack 4 application possible.
|
||||
Check out
|
||||
[src/application.ts](https://github.com/loopbackio/loopback-next/blob/master/examples/hello-world/src/application.ts)
|
||||
to learn how we configured our application to always respond with "Hello
|
||||
World!".
|
||||
|
||||
## Prerequisites
|
||||
|
||||
Before we can begin, you'll need to make sure you have some things installed:
|
||||
|
||||
- [Node.js](https://nodejs.org/en/) at v10 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.
|
||||
|
||||
```sh
|
||||
npm i -g @loopback/cli
|
||||
```
|
||||
|
||||
2. Download the "hello-world" application.
|
||||
|
||||
```sh
|
||||
lb4 example hello-world
|
||||
```
|
||||
|
||||
3. Switch to the directory.
|
||||
|
||||
```sh
|
||||
cd loopback4-example-hello-world
|
||||
```
|
||||
|
||||
## Use
|
||||
|
||||
Start the app:
|
||||
|
||||
```sh
|
||||
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!`.
|
||||
|
||||
## Contributions
|
||||
|
||||
- [Guidelines](https://github.com/loopbackio/loopback-next/blob/master/docs/CONTRIBUTING.md)
|
||||
- [Join the team](https://github.com/loopbackio/loopback-next/issues/110)
|
||||
|
||||
## Tests
|
||||
|
||||
Run `npm test` from the root folder.
|
||||
|
||||
## Contributors
|
||||
|
||||
See
|
||||
[all contributors](https://github.com/loopbackio/loopback-next/graphs/contributors).
|
||||
|
||||
## License
|
||||
|
||||
MIT
|
||||
|
||||
69
package.json
69
package.json
@ -1,26 +1,61 @@
|
||||
{
|
||||
"name": "loopback-app",
|
||||
"version": "1.0.0",
|
||||
"description": "Simple LoopBack 4 application",
|
||||
"name": "@loopback/example-hello-world",
|
||||
"description": "A simple hello-world Application using LoopBack 4",
|
||||
"version": "7.0.7",
|
||||
"keywords": [
|
||||
"loopback",
|
||||
"LoopBack",
|
||||
"example",
|
||||
"tutorial"
|
||||
],
|
||||
"license": "MIT",
|
||||
"main": "dist/index.js",
|
||||
"types": "dist/index.d.ts",
|
||||
"author": "IBM Corp. and LoopBack contributors",
|
||||
"copyright.owner": "IBM Corp. and LoopBack contributors",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/loopbackio/loopback-next.git",
|
||||
"directory": "examples/hello-world"
|
||||
},
|
||||
"engines": {
|
||||
"node": "18 || 20 || 22"
|
||||
},
|
||||
"scripts": {
|
||||
"acceptance": "lb-mocha \"dist/__tests__/acceptance/**/*.js\"",
|
||||
"build": "lb-tsc",
|
||||
"start": "node -r source-map-support/register .",
|
||||
"clean": "rimraf dist",
|
||||
"prestart": "npm run build"
|
||||
"build:watch": "lb-tsc --watch",
|
||||
"clean": "lb-clean *example-hello-world*.tgz dist *.tsbuildinfo package",
|
||||
"verify": "npm pack && tar xf *example-hello-world*.tgz && tree package && npm run clean",
|
||||
"lint": "npm run prettier:check && npm run eslint",
|
||||
"lint:fix": "npm run eslint:fix && npm run prettier:fix",
|
||||
"prettier:cli": "lb-prettier \"**/*.ts\" \"**/*.js\"",
|
||||
"prettier:check": "npm run prettier:cli -- -l",
|
||||
"prettier:fix": "npm run prettier:cli -- --write",
|
||||
"eslint": "lb-eslint --report-unused-disable-directives .",
|
||||
"eslint:fix": "npm run eslint -- --fix",
|
||||
"pretest": "npm run rebuild",
|
||||
"test": "lb-mocha --allow-console-logs \"dist/__tests__/**/*.js\"",
|
||||
"posttest": "npm run lint",
|
||||
"test:dev": "lb-mocha --allow-console-logs dist/__tests__/**/*.js && npm run posttest",
|
||||
"rebuild": "npm run clean && npm run build",
|
||||
"prestart": "npm run rebuild",
|
||||
"start": "node ."
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
"dependencies": {
|
||||
"@loopback/boot": "^5.0.0",
|
||||
"@loopback/core": "^5.0.0",
|
||||
"@loopback/rest": "^5.0.0",
|
||||
"@loopback/repository": "^5.0.0",
|
||||
"@loopback/rest-explorer": "^5.0.0",
|
||||
"source-map-support": "^0.5.21"
|
||||
"@loopback/core": "^6.1.4",
|
||||
"@loopback/rest": "^14.0.7",
|
||||
"tslib": "^2.6.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@loopback/build": "^6.0.0",
|
||||
"rimraf": "^5.0.0",
|
||||
"typescript": "^5.2.2"
|
||||
},
|
||||
"license": "MIT"
|
||||
"@loopback/build": "^11.0.6",
|
||||
"@loopback/eslint-config": "^15.0.4",
|
||||
"@loopback/testlab": "^7.0.6",
|
||||
"@types/node": "^16.18.119",
|
||||
"eslint": "^8.57.0",
|
||||
"typescript": "~5.2.2"
|
||||
}
|
||||
}
|
||||
|
||||
38
src/__tests__/acceptance/application.acceptance.ts
Normal file
38
src/__tests__/acceptance/application.acceptance.ts
Normal file
@ -0,0 +1,38 @@
|
||||
// Copyright IBM Corp. and LoopBack contributors 2019. 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 {
|
||||
Client,
|
||||
createRestAppClient,
|
||||
expect,
|
||||
givenHttpServerConfig,
|
||||
} from '@loopback/testlab';
|
||||
import {HelloWorldApplication} from '../../application';
|
||||
|
||||
describe('Application', () => {
|
||||
let app: HelloWorldApplication;
|
||||
let client: Client;
|
||||
|
||||
before(givenAnApplication);
|
||||
before(async () => {
|
||||
await app.start();
|
||||
client = createRestAppClient(app);
|
||||
});
|
||||
after(async () => {
|
||||
await app.stop();
|
||||
});
|
||||
|
||||
it('responds with hello world', async () => {
|
||||
const response = await client.get('/').expect(200);
|
||||
expect(response.text).to.eql('Hello World!');
|
||||
});
|
||||
|
||||
function givenAnApplication() {
|
||||
app = new HelloWorldApplication({
|
||||
rest: givenHttpServerConfig(),
|
||||
disableConsoleLog: true,
|
||||
});
|
||||
}
|
||||
});
|
||||
@ -1,16 +1,33 @@
|
||||
import {ApplicationConfig} from '@loopback/core';
|
||||
import {RestApplication} from '@loopback/rest';
|
||||
import {RestExplorerBindings} from '@loopback/rest-explorer';
|
||||
import {PingController} from './controllers/ping.controller';
|
||||
// Copyright IBM Corp. and LoopBack contributors 2018,2020. 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 class Application extends RestApplication {
|
||||
import {ApplicationConfig} from '@loopback/core';
|
||||
import {RestApplication, RestServer} from '@loopback/rest';
|
||||
export {ApplicationConfig};
|
||||
|
||||
export class HelloWorldApplication extends RestApplication {
|
||||
constructor(options: ApplicationConfig = {}) {
|
||||
super(options);
|
||||
this.projectRoot = __dirname;
|
||||
this.controller(PingController);
|
||||
this.bind(RestExplorerBindings.CONFIG).to({
|
||||
path: '/explorer',
|
||||
|
||||
// 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(({response}, sequence) => {
|
||||
sequence.send(response, 'Hello World!');
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async start() {
|
||||
await super.start();
|
||||
|
||||
if (!this.options?.disableConsoleLog) {
|
||||
const rest = await this.getServer(RestServer);
|
||||
console.log(
|
||||
`REST server running on port: ${await rest.get('rest.port')}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,9 +0,0 @@
|
||||
import {get} from '@loopback/rest';
|
||||
|
||||
export class PingController {
|
||||
@get('/ping')
|
||||
ping(): object {
|
||||
return {message: 'pong'};
|
||||
}
|
||||
}
|
||||
|
||||
36
src/index.ts
36
src/index.ts
@ -1,14 +1,30 @@
|
||||
import {Application} from './application';
|
||||
// Copyright IBM Corp. and LoopBack contributors 2018,2020. 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 async function main() {
|
||||
const app = new Application();
|
||||
await app.boot();
|
||||
import {ApplicationConfig, HelloWorldApplication} from './application';
|
||||
|
||||
export async function main(config: ApplicationConfig) {
|
||||
const app = new HelloWorldApplication();
|
||||
await app.start();
|
||||
console.log(`Application is running at http://127.0.0.1:3000`);
|
||||
return app;
|
||||
}
|
||||
|
||||
main().catch(err => {
|
||||
console.error('Cannot start the application.', err);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
if (require.main === module) {
|
||||
// Run the application
|
||||
const config = {
|
||||
rest: {
|
||||
port: +(process.env.PORT ?? 3000),
|
||||
host: process.env.HOST ?? 'localhost',
|
||||
openApiSpec: {
|
||||
// useful when used with OpenAPI-to-GraphQL to locate your application
|
||||
setServersFromRequest: true,
|
||||
},
|
||||
},
|
||||
};
|
||||
main(config).catch(err => {
|
||||
console.error('Cannot start the application.', err);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
|
||||
@ -1,35 +0,0 @@
|
||||
import {
|
||||
SequenceHandler,
|
||||
FindRoute,
|
||||
ParseParams,
|
||||
InvokeMethod,
|
||||
Send,
|
||||
Reject,
|
||||
RequestContext,
|
||||
} from '@loopback/rest';
|
||||
|
||||
export class MySequence implements SequenceHandler {
|
||||
constructor(
|
||||
public findRoute: FindRoute,
|
||||
public parseParams: ParseParams,
|
||||
public invoke: InvokeMethod,
|
||||
public send: Send,
|
||||
public reject: Reject,
|
||||
) {}
|
||||
|
||||
async handle(context: RequestContext): Promise<void> {
|
||||
const {request, response} = context;
|
||||
try {
|
||||
const route = this.findRoute(request);
|
||||
|
||||
const args = await this.parseParams(request, route);
|
||||
|
||||
const result = await this.invoke(route, args);
|
||||
|
||||
this.send(response, result);
|
||||
} catch (error) {
|
||||
this.reject(context, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,24 @@
|
||||
{
|
||||
"extends": "./node_modules/@loopback/build/config/tsconfig.common.json",
|
||||
"include": ["src"]
|
||||
"$schema": "https://json.schemastore.org/tsconfig",
|
||||
"extends": "@loopback/build/config/tsconfig.common.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "dist",
|
||||
"rootDir": "src",
|
||||
"composite": true
|
||||
},
|
||||
"include": [
|
||||
"src/**/*",
|
||||
"src/**/*.json"
|
||||
],
|
||||
"references": [
|
||||
{
|
||||
"path": "../../packages/core/tsconfig.json"
|
||||
},
|
||||
{
|
||||
"path": "../../packages/rest/tsconfig.json"
|
||||
},
|
||||
{
|
||||
"path": "../../packages/testlab/tsconfig.json"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user