### Install and Run LoopBack Mixins Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-mixins.md Instructions for cloning the repository, installing dependencies, and starting the example application. ```bash $ git clone https://github.com/strongloop/loopback-example-mixins $ cd loopback-example-mixins $ npm install $ npm start ``` -------------------------------- ### Install and Run LoopBack Pub-Sub Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-pubsub.md Follow these steps to clone the repository, install dependencies, start the build process, run the Mosquitto server, and launch the LoopBack application. ```bash $ git clone https://github.com/strongloop/loopback-example-pubsub.git $ cd loopback-example-pubsub $ npm install $ gulp # manually exit via ctrl+c here, it is a known issue ATM $ mosquitto & # run the mosquitto server $ node server/server.js ``` -------------------------------- ### Start the Example Application Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/express-with-lb4-rest-tutorial.md Start the example application after cloning and navigating to its directory. ```sh npm start Server is running at http://127.0.0.1:3000 ``` -------------------------------- ### Clone, Install, and Start LoopBack 4 Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/Preparing-the-API-for-consumption.shelved.md Use these commands to set up and run the loopback4-example-todo application to test your API. ```sh lb4 example todo cd loopback4-example-todo npm start ``` -------------------------------- ### Install and Run LoopBack Middleware Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-middleware.md Clone the repository, install dependencies, and run the example application. ```bash $ git clone git@github.com:strongloop/loopback-example-middleware.git $ cd loopback-example-middleware $ npm install $ node . ``` -------------------------------- ### Clone and Install LoopBack Passport Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-passport.md Clone the example repository and install its dependencies to get started. ```bash $ git clone git@github.com:strongloop/loopback-example-passport.git $ cd loopback-example-passport $ npm install ``` -------------------------------- ### Install and Run LoopBack User Management Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-user-management.md Commands to clone the repository, install dependencies, and run the example project. ```bash $ git clone git@github.com:strongloop/loopback-example-user-management.git $ cd loopback-example-user-management $ npm install $ node . ``` -------------------------------- ### Install LoopBack 4 Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-next/examples/passport-login/README.md Installs the passport-login example locally using the lb4 example command. ```sh lb4 example passport-login ``` -------------------------------- ### Navigate to Example Directory Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-next/examples/soap-calculator/README.md Change the current directory to the cloned example project. This is necessary before installing dependencies or starting the application. ```sh cd loopback4-example-soap-calculator ``` -------------------------------- ### Clone and Run Strong-PubSub Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/strong-pubsub-example.md Clone the repository, navigate to the directory, install dependencies, and run the server to start the example. ```bash git clone https://github.com/strongloop/strong-pubsub-example.git cd strong-pubsub-example npm install node server.js ``` -------------------------------- ### Install Dependencies and Start Server Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-connector-remote.md Install npm dependencies for both client and server, then start the LoopBack server application. ```bash cd client npm install cd ../server npm install node . ``` -------------------------------- ### Run Example Application Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-next/examples/access-control-migration/README.md Starts the example LoopBack 4 application. After starting, the server will be accessible at http://127.0.0.1:3000. ```sh $ npm start Server is running at http://127.0.0.1:3000 ``` -------------------------------- ### Install SOAP Connector Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-connector-soap.md Clone the repository and install npm dependencies to set up the example application. ```shell git clone https://github.com/strongloop-community/loopback-example-connector.git -b soap cd loopback-example-connector npm install ``` -------------------------------- ### Basic LoopBack Application Setup Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-next/packages/core/README.md Initialize a LoopBack application and mount REST and gRPC components. This example demonstrates how to configure ports and start the application, automatically spinning up all mounted servers. ```typescript import { Application } from '@loopback/core'; import { RestComponent } from '@loopback/rest'; import { GrpcComponent } from '@loopback/grpc'; const app = new Application({ rest: { port: 3000, }, grpc: { port: 3001, }, }); app.component(RestComponent); // REST Server app.component(GrpcComponent)( // GRPC Server async function start() { // Let's retrieve the bound instances of our servers. const rest = await app.getServer('RestServer'); const grpc = await app.getServer('GrpcServer'); // Define all sorts of bindings here to pass configuration or data // between your server instances, define controllers and datasources for them, // etc... await app.start(); // This automatically spins up all your servers, too! console.log(`REST server running on port: ${rest.getSync('rest.port')}`); console.log(`GRPC server running on port: ${grpc.getSync('grpc.port')}`); }, )(); ``` -------------------------------- ### Download Hello World Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-next/examples/hello-world/README.md Downloads the 'hello-world' LoopBack 4 example application. Use this command to get a starter project. ```sh lb4 example hello-world ``` -------------------------------- ### Fetch LoopBack Readmes Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/README.md Install the `get-readmes` utility and run the script to fetch README files from LoopBack example repositories. This is typically done once for setup. ```bash npm install npm run fetch-readmes ``` -------------------------------- ### Install and Start LoopBack Application Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/Creating-servers.md Install project dependencies and start the LoopBack application using npm commands. ```bash npm i && npm start ``` -------------------------------- ### Install Dependencies Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-next/examples/passport-login/README.md Changes into the example directory and installs the required npm dependencies. ```sh cd loopback4-example-passport-login && npm i ``` -------------------------------- ### Start the Binding Resolution Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-next/examples/binding-resolution/README.md Use this command to start the example application. Set the DEBUG environment variable to see detailed logs. You can also force binding scope to 'transient' by setting BINDING_SCOPE=transient. ```sh DEBUG=loopback:example:binding-resolution npm start ``` ```sh DEBUG=loopback:example:binding-resolution BINDING_SCOPE=transient npm start ``` -------------------------------- ### Clone and Test Redis Connector Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-kv-connectors.md Instructions to clone the repository, navigate to the Redis connector example for LoopBack 2.x, install dependencies, start the Redis server, and start the application. ```shell git clone https://github.com/strongloop/loopback-example-kv-connectors.git cd loopback-example-kv-connectors/redis-lb2x/ npm install redis-server --daemonize yes # starts redis in the background npm start ``` -------------------------------- ### Clone and Run LoopBack Access Control Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-access-control.md Instructions to clone the example project from GitHub, install dependencies, and run the application. ```bash $ git clone https://github.com/strongloop/loopback-example-access-control $ cd loopback-example-access-control $ npm install $ node . ``` -------------------------------- ### Clone and Run LoopBack Storage Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-storage.md Follow these steps to clone the repository, install dependencies, and run the LoopBack 3.0 storage example application. ```bash $ git clone https://github.com/strongloop/loopback-example-storage.git $ cd loopback-example-storage/example-3.0 $ npm install $ node . ``` -------------------------------- ### Start LoopBack 4 Project Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/Getting-started.md Navigate to your project directory, install dependencies, and start the application to test the default 'ping' route. ```sh cd getting-started npm i npm start ``` -------------------------------- ### Clone and Run LoopBack Isomorphic Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-isomorphic.md Follow these commands to clone the repository, install dependencies, and run the isomorphic LoopBack example. ```bash $ git clone https://github.com/strongloop/loopback-example-isomorphic.git $ cd loopback-example-isomorphic $ npm install $ node . ``` -------------------------------- ### Example Controller with Methods Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/Controller.md This example demonstrates a LoopBack controller with a method decorated with `@get` for handling HTTP GET requests and `@param.query` for query parameters. ```APIDOC ## GET /messages ### Description Retrieves a list of messages, with an optional limit on the number of items returned. ### Method GET ### Endpoint /messages ### Parameters #### Query Parameters - **limit** (number) - Optional - The maximum number of messages to return. Defaults to 10. If greater than 100, it will be capped at 100. ### Response #### Success Response (200) - **HelloMessage[]** - An array of HelloMessage objects. ### Request Example ```http GET /messages?limit=50 ``` ### Response Example ```json [ { "id": 1, "text": "Hello World" } ] ``` ``` -------------------------------- ### Download LoopBack 4 Example using CLI Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/Examples.md Use the `lb4 example` command to download a specific LoopBack 4 example project. Ensure you have the LoopBack 4 CLI installed. ```sh lb4 example ``` ```sh lb4 example hello-world ``` -------------------------------- ### Clone and Run LoopBack Relations Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-relations.md Clone the example repository, install dependencies, and run the application to explore model relations. ```bash $ git clone https://github.com/strongloop/loopback-example-relations.git $ cd loopback-example-relations $ npm install $ node . ``` -------------------------------- ### Run Demo Project Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-next/extensions/metrics/README.md Clone the loopback-next repository, install dependencies, build the project, navigate to the metrics-prometheus example, and run the demo. ```sh git clone https://github.com/loopbackio/loopback-next npm install npm run build cd examples/metrics-prometheus npm run demo ``` -------------------------------- ### Run the LoopBack 4 application Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/tutorials/soap-calculator/soap-calculator-tutorial-scaffolding.md After scaffolding, navigate to the project directory and start the application using 'npm start'. Dependencies are automatically installed. ```sh cd soap-calculator npm start ``` -------------------------------- ### Clone Todo List Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-next/examples/todo-list/README.md Clones the 'todo-list' example repository using the `lb4 example` command. This is the first step to get the example application running. ```sh lb4 example todo-list ``` -------------------------------- ### Download LoopBack 4 Tutorial Project using CLI Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/Tutorials.md Use the `lb4 example` command to download a tutorial project. Ensure you have the LoopBack 4 CLI installed. ```sh lb4 example ``` -------------------------------- ### LoopBack 4 Application Setup Prompts Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/blog/building-online-game-with-loopback-4-pt1.html Example of prompts encountered when initializing a LoopBack 4 project via the CLI. ```bash wenbo:firstgameDemo wenbo$ lb4 app? Project name: firstgame? Project description: firstgameDemo? Project root directory: firstgame? Application class name: FirstgameApplication? Select features to enable in the project Enable tslint, Enable prettier,Enable mocha, Enable loopbackBuild, Enable vscode, Enable repositories, Enable services ``` -------------------------------- ### Basic strong-remoting server setup Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/strong-remoting.md Set up a basic strong-remoting server with a single remote method 'user.greet'. This example demonstrates creating a remoting collection, defining a shared class, mapping a method, and exposing it over the REST transport. ```js // Create a collection of remote objects. var remoting = require('../'); var SharedClass = remoting.SharedClass var remotes = remoting.create(); // define a class-like object (or constructor) function User() { } User.greet = function (fn) { fn(null, 'hello, world!'); } // create a shared class to allow strong-remoting to map // http requests to method invocations on your class var userSharedClass = new SharedClass('user', User); // Tell strong-remoting about your greet method userSharedClass.defineMethod('greet', { isStatic: true, // not an instance method returns: [{ arg: 'msg', type: 'string' // define the type of the callback arguments }] }); // Expose it over the REST transport. require('http') .createServer(remotes.handler('rest')) .listen(3000); ``` -------------------------------- ### Clone and Run LoopBack Example App Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-app-logic.md Clone the repository, install dependencies, and run the application. Then, test remote methods using provided request scripts. ```bash $ git clone https://github.com/strongloop/loopback-example-app-logic.git $ cd loopback-example-app-logic $ npm install $ node . # then in a different tab, run ./bin/remote-method-request or ./bin/datetime-request ``` -------------------------------- ### Clone and Run LoopBack Angular Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-angular.md Clone the repository, install dependencies, and run the LoopBack server. Then access the application in your browser. ```bash $ git clone https://github.com/strongloop/loopback-example-angular.git $ cd loopback-example-angular $ npm install $ node . # then browse to localhost:3000 ``` -------------------------------- ### Setup RestServer and HTTP Server Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/apidocs/rest.restserver.oasenhancerservice.md This example demonstrates how to set up a LoopBack 4 application with the RestComponent, retrieve the RestServer instance, and create an HTTP server to listen for requests. ```typescript const app = new Application(); app.component(RestComponent); // setup controllers, etc. const restServer = await app.getServer(RestServer); const httpServer = http.createServer(restServer.requestHandler); httpServer.listen(3000); ``` -------------------------------- ### Create and Start HTTP Server with RestApplication Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/apidocs/rest.restapplication.requesthandler.md Instantiate a `RestApplication`, set up controllers, and then create an HTTP server using the application's `requestHandler`. This example demonstrates how to make your LoopBack API accessible via HTTP. ```typescript const app = new RestApplication(); // setup controllers, etc. const server = http.createServer(app.requestHandler); server.listen(3000); ``` -------------------------------- ### Navigate to Example Directory Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/express-with-lb4-rest-tutorial.md Switch to the cloned example's directory. ```sh cd loopback4-example-express-composition ``` -------------------------------- ### Clone the references-many Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-next/examples/references-many/README.md Use the `lb4 example` command to clone the repository for this example. ```sh lb4 example references-many ``` -------------------------------- ### Navigate to the Example Directory Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-next/examples/references-many/README.md Change into the cloned example's directory. ```sh cd loopback4-example-references-many ``` -------------------------------- ### Navigate to Example Directory Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-next/examples/hello-world/README.md Changes the current directory to the downloaded 'hello-world' example project. This is necessary before running application commands. ```sh cd loopback4-example-hello-world ``` -------------------------------- ### Download example project Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-next/packages/context/README.md Use the lb4 CLI to download a standalone example project for @loopback/context. ```sh lb4 example context ``` -------------------------------- ### Install Passport HTTP Strategy Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-next/extensions/authentication-passport/README.md Install passport-http and its types for usage examples. This is required for the examples that follow in the documentation. ```sh npm i passport-http @types/passport-http --save ``` -------------------------------- ### Navigate to Example Directory Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-next/examples/todo/README.md Change the current directory to the cloned todo example project. ```sh cd loopback4-example-todo ``` -------------------------------- ### Clone and Run the Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-connector-rest.md Instructions to clone the repository, checkout the REST connector branch, and run both the external and local servers. ```bash $ git clone https://github.com/strongloop-community/loopback-example-connector.git $ cd loopback-example-connector $ git checkout rest $ cd external-server $ npm install $ node . ``` ```bash $ cd local-server $ npm install $ node . ``` -------------------------------- ### Appsody Docker Run Output Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/Appsody-LoopBack.md This output shows the steps Appsody takes to build and run your LoopBack 4 application, including Docker image pulling, container setup, dependency installation, and application startup. ```text Running development environment... Pulling docker image appsody/nodejs-loopback:0.1 Running command: docker pull appsody/nodejs-loopback:0.10.1: Pulling from appsody/nodejs-loopback Digest: sha256:c810188750a998bbc84a3dba1a2bb04413851453da6874d224a129994b12c9c5 Status: Image is up to date for appsody/nodejs-loopback:0.1 docker.io/appsody/nodejs-loopback:0.1 Running command: docker run --rm -p 3000:3000 -p 9229:9229 --name appsodylb4todo-dev -v /Users/dremond/Documents/appsody_stuff/appsodyLB4Todo/:/project/user-app -v appsodylb4todo-deps:/pr oject/user-app/node_modules -v appsody-controller-0.3.1:/.appsody -t --entrypoint /.appsody/appsody-controller appsody/nodejs-loopback:0.1 --mode=run [Container] Running APPSODY_PREP command: npm install --prefix user-app && npm run build --prefix user-app npm WARN nodejs-loopback-scaffold@1.0.0 No license field. [Container] audited 4510 packages in 6.142s [Container] [Container] > nodejs-loopback-scaffold@1.0.0 build /project/user-app [Container] > lb-tsc [Container] Running command: npm start [Container] [Container] > nodejs-loopback@0.1.7 start /project [Container] > node -r source-map-support/register . [Container] [Container] Server is running at http://[::1]:3000 [Container] Try http://[::1]:3000/ping ``` -------------------------------- ### Start OpenAPI-to-GraphQL Server Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/getting-started-openapi-to-graphql.html After installing OpenAPI-to-GraphQL and obtaining the OAS, start the GraphQL server from the OpenAPI-to-GraphQL installation folder. The server will be accessible at http://127.0.0.1:3001/graphql. ```bash # in the OpenAPI-to-GraphQL folder: openapi-to-graphql ``` -------------------------------- ### Install Dependencies and Navigate Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/README.md Navigate to the cloned repository directory and install the necessary gems using Bundler. ```bash $ cd loopback.io $ bundle install ``` -------------------------------- ### Install Model API builder package Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/Extending-Model-API-builder.md Install the necessary package to start creating your own API builders. ```sh npm install --save @loopback/model-api-builder ``` -------------------------------- ### Run Example LoopBack Server Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-component-push.md Commands to set up and run the example LoopBack server for push notifications. By default, it uses an in-memory store. ```shell cd example/server npm install bower install node app ``` -------------------------------- ### Application.state Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/apidocs/core.application.state.md Gets the current state of the application. The state transitions through 'created', 'starting', 'started', 'stopping', and 'stopped'. Operations like 'start' and 'stop' can only be invoked when the application is in a stable state ('started' or 'stopped'). ```APIDOC ## Application.state ### Description Get the state of the application. The initial state is `created` and it can transition as follows by `start` and `stop`: 1. start - !started -> starting -> started -> started (no-op) 2. stop - (started || initialized) -> stopping -> stopped -> ! (started || initialized) -> stopped (no-op) Two types of states are expected: - stable, such as `started` and `stopped` - in process, such as `booting` and `starting` Operations such as `start` and `stop` can only be called at a stable state. The logic should immediately set the state to a new one indicating work in process, such as `starting` and `stopping`. ### Signature ```typescript get state(): string; ``` ``` -------------------------------- ### Install Node and Bower Dependencies Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-angular-live-set.md Installs the necessary Node.js and Bower packages for the LoopBack Angular Live Set example. ```bash npm install && bower install # make sure you have gulp installed globally... or npm install gulp -g ``` -------------------------------- ### Run Authentication Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-connector-remote.md Execute the auth.js example to demonstrate user registration, login, custom remote method invocation, and logout using the remote connector. ```bash node examples/auth.js ``` -------------------------------- ### Build and Run the Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-angular-live-set.md Builds and serves the LoopBack Angular Live Set example using Gulp. Navigate to http://localhost:3000 in two separate browser windows to observe real-time updates. ```bash gulp serve ``` -------------------------------- ### Create Sample Model Instances Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-access-control.md A boot script to create initial data for users, projects, and roles, including setting up an administrator role. ```javascript module.exports = function(app) { app.dataSources.db.automigrate('User', function(err) { if (err) throw err; app.models.User.create([ { name: 'John', email: 'john@example.com', password: 'password' }, { name: 'Jane', email: 'jane@example.com', password: 'password' }, { name: 'Bob', email: 'bob@example.com', password: 'password' } ], function(err, users) { if (err) throw err; app.models.Project.create([ { name: 'Project 1', ownerId: users[0].id, balance: 1000 }, { name: 'Project 2', ownerId: users[1].id, balance: 500 } ], function(err, projects) { if (err) throw err; // Add John and Jane to Project 1's team projects[0].members.add(users[0], function(err) { if (err) throw err; projects[0].members.add(users[1], function(err) { if (err) throw err; }); }); // Add Jane to Project 2's team (as solo member) projects[1].members.add(users[1], function(err) { if (err) throw err; }); }); }); }); // Create an 'admin' role and assign Bob to it app.models.Role.create({ name: 'admin' }, function(err, role) { if (err) throw err; app.models.RoleMapping.create({ principalType: app.models.RoleMapping.USER, principalId: 3, // Assuming Bob is the 3rd user created (index 2, ID 3) roleId: role.id }, function(err, roleMapping) { if (err) throw err; }); }); }; ``` -------------------------------- ### REST Query Example: Category Starts with 'T' Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/Where-filter.md Retrieve products where the category starts with a capital 'T' via the REST API using a regular expression. ```http /api/products?filter[where][category][regexp]=^T ``` -------------------------------- ### REST Query Example: Case-Insensitive Category Start Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/Where-filter.md Retrieve products where the category starts with 't' or 'T' using the REST API. A regular expression with the 'i' flag is used. ```http /api/products?filter[where][category][regexp]=/^t/i ``` -------------------------------- ### Query Example: Category Starts with 'T' (Node.js API) Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/Where-filter.md Find all products where the category property starts with a capital 'T' using the Node.js API with a regular expression. ```typescript await productRepository.find({where: {category: {regexp: '^T'}}}); ``` -------------------------------- ### Create a New LoopBack Application Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/getting-started/index.html Use the LoopBack CLI to generate a new 'Hello World' application. Follow the prompts to configure your project. ```bash $ lb ? What's the name of your application? hello-world ? Enter name of the directory to contain the project: hello-world ? Which version of LoopBack would you like to use? 3.x (Active Long Term Support) ? What kind of application do you have in mind? hello-world (A project containing a controller, including a single vanilla Message and a single remote method) ...I'm all done. Running npm install for you to install the required dependencies. If this fails, try running the command yourself. ... ``` -------------------------------- ### Basic Ping Controller Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/Accessing-http-request-response.md A standard LoopBack 4 controller demonstrating how to define a GET endpoint with OpenAPI responses. This example does not directly access request/response objects. ```typescript import {get} from '@loopback/rest'; import {inject} from '@loopback/core'; export class PingController { constructor() {} // Map to `GET /ping` @get('/ping', { responses: { '200': { description: 'Ping Response', content: { 'application/json': { schema: { type: 'object', title: 'PingResponse', properties: { greeting: {type: 'string'}, date: {type: 'string'}, }, }, }, }, }, }, }) ping(@param.query.string('message') msg: string): object { return { greeting: `[Pong] ${msg}`, date: new Date(), }; } } ``` -------------------------------- ### Clone the Example Repository Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-xamarin.md Use this command to download the example application code from GitHub. ```bash $ git clone https://github.com/strongloop/loopback-example-xamarin.git ``` -------------------------------- ### Clone and Checkout Repository Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-connector-remote.md Clone the repository and checkout the 'remote' branch to get the example code. ```bash git clone https://github.com/strongloop/loopback-example-connector.git cd loopback-example-connector git checkout remote ``` -------------------------------- ### Prepare Documentation Preview Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/DEVELOPING.md Run this command once to set up the preview environment for LoopBack 4 documentation. It creates a Jekyll project and configures symlinks for live updates. ```sh $ npm run docs:prepare ``` -------------------------------- ### Simple Binding Filter Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/apidocs/context.bindingfilter.md A straightforward example of a binding filter function that checks if a binding's key starts with 'services'. This demonstrates the simplicity achieved by using a boolean return type. ```typescript b => b.key.startsWith('services') ``` -------------------------------- ### Download RPC Server Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-next/examples/rpc-server/README.md Download the 'rpc-server' application template using the LoopBack CLI. ```sh lb4 example rpc-server ``` -------------------------------- ### Query Example: Category Starts with 'T' (Node.js API - Simplified) Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/Where-filter.md Find all products where the category property starts with a capital 'T' using the simplified Node.js API syntax for regular expressions. ```typescript await productRepository.find({where: {category: /^T/}}); ``` -------------------------------- ### Start Local MQ Light Server Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-connector-mqlight.md Start a local IBM MQ Light server instance. This command assumes the MQ Light installation is available in your environment and the MQLIGHT_HOME variable is set. ```shell $ $MQLIGHT_HOME/mqlight-start ``` -------------------------------- ### Clone and Run LoopBack Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-database-mssql.md Clone the LoopBack database example repository and run the application. ```bash git clone https://github.com/strongloop/loopback-example-database cd loopback-example-database npm install npm start ``` -------------------------------- ### Synchronous LoopBack 3 Boot Script Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/migration/boot-scripts.md A synchronous boot script that accesses and logs the name of a datasource before the application starts. This pattern is directly translatable to an observer's start function in LoopBack 4. ```javascript 'use strict'; module.exports = function printInfo(server) { // SUPPOSE your application has a datasource called `db`, which is usually the default // memory datasource in a sample LoopBack application created by CLI, regardless if it's // an LB3 app or LB4 app const db = server.datasources.db; console.log('This is a synchronous script.'); console.log('Your app has a datasource called: ', db.name); }; ``` -------------------------------- ### Example: Debug Loopback Datasource (Windows) Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/Setting-debug-strings.md This Windows example shows how to set the DEBUG environment variable to 'loopback:datasource' to view logs related to data source operations. Run 'npm start' afterwards to apply the setting. ```shell C:\> set DEBUG=loopback:datasource C:\> npm start ``` -------------------------------- ### Example lb4 update output Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/Update-generator.md This output shows an example of running `lb4 update` after installing a newer version of `@loopback/cli`. It lists incompatible dependencies, prompts for upgrade, and details the dependency changes, including potential overwrites of `package.json`. ```sh The project was originally generated by @loopback/cli@1.0.1. The following dependencies are incompatible with @loopback/cli@1.24.0: - @types/node: ^10.14.6 (cli ^10.17.3) - @loopback/boot: ^1.2.7 (cli ^1.5.10) - @loopback/build: ^1.5.4 (cli ^2.0.15) - @loopback/core: ^1.7.0 (cli ^1.10.6) - @loopback/repository: ^1.5.5 (cli ^1.15.3) - @loopback/rest: ^1.11.2 (cli ^1.22.0) - @loopback/testlab: ^1.2.9 (cli ^1.9.3) - @loopback/service-proxy: ^1.1.10 (cli ^1.3.10) ? How do you want to proceed? Upgrade project dependencies - Dependency @loopback/boot: ^1.2.7 => ^1.5.10 - Dependency @loopback/core: ^1.7.0 => ^1.10.6 - Dependency @loopback/repository: ^1.5.5 => ^1.15.3 - Dependency @loopback/rest: ^1.11.2 => ^1.22.0 - Dependency @loopback/service-proxy: ^1.1.10 => ^1.3.10 - DevDependency @loopback/build: ^1.5.4 => ^2.0.15 - DevDependency @loopback/testlab: ^1.2.9 => ^1.9.3 - DevDependency @types/node: ^10.14.6 => ^10.17.3 Upgrading dependencies may break the current project. conflict package.json ? Overwrite package.json? overwrite force package.json ``` -------------------------------- ### Create a new React project Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/blog/building-an-online-game-with-loopback-4-pt6.html Run this command after installing `create-react-app` to initialize a new React project with a specified name. This sets up the basic file structure and build tools. ```bash create-react-app ``` -------------------------------- ### Create and Start HttpCachingProxy Instance Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-next/packages/http-caching-proxy/README.md Create a new instance of HttpCachingProxy during test suite setup, typically in a 'before' hook. Ensure the cachePath is an absolute path. The proxy will start listening on a random port if port is set to 0. ```ts const proxy = new HttpCachingProxy({ // directory where to store recorded snapshots - required cachePath: path.resolve(__dirname, '.proxy-cache'), // port where to listen - 0 by default port: 0, // how often to re-validate snapshots (in milliseconds) - one day by default ttl: 24 * 60 * 60 * 1000, }); await proxy.start(); ``` -------------------------------- ### Routing to Controllers with Decorators Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/Controller.md This example shows how to use the `@get` decorator to automatically route HTTP requests to controller methods. ```APIDOC ## Routing with Decorators ### Description Uses the `@get` decorator to define routing metadata for a controller method, allowing LoopBack to automatically register the route. ### Code ```ts import {get} from '@loopback/rest'; class MyController { @get('/greet', spec) // 'spec' is an OpenAPI Operation Object greet(name: string) { return `hello ${name}`; } } // In your application constructor: this.controller(MyController); ``` ``` -------------------------------- ### Setup Dockerized Oracle Instance Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-connector-oracle.md Use the 'setup.sh' script to spawn a Dockerized Oracle instance, with optional parameters for host, port, user, and password. ```bash source setup.sh ``` -------------------------------- ### Start the LoopBack 4 Project Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/getting-started.html Navigate to your project directory and start the application using npm. The default 'ping' route can be tested in a browser. ```bash cd getting-started npm start ``` -------------------------------- ### Setup MSSQL Instance with Docker Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-connector-mssql.md Run this script to set up a MSSQL instance using Docker. Optional parameters can be provided for host, port, user, password, and database. ```bash source setup.sh ``` -------------------------------- ### Query Customers with Orders via Controller Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/blog/inclusion-of-related-models.html Example of how to query all customers and include their related orders using a GET request with a filter parameter. ```http GET http://localhost:3000/customers?filter[include][][relation]=orders ``` -------------------------------- ### Implement Boot and Start Functions Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/express-with-lb4-rest-tutorial.md Add methods to the ExpressServer class for booting the LoopBack application and starting the Express server, including graceful shutdown handling. ```typescript import {once} from 'events'; export class ExpressServer { public readonly app: express.Application; public readonly lbApp: NoteApplication; private server?: http.Server; constructor(options: ApplicationConfig = {}) { //... } async boot() { await this.lbApp.boot(); } public async start() { await this.lbApp.start(); const port = this.lbApp.restServer.config.port ?? 3000; const host = this.lbApp.restServer.config.host || '127.0.0.1'; this.server = this.app.listen(port, host); await once(this.server, 'listening'); } // For testing purposes public async stop() { if (!this.server) return; await this.lbApp.stop(); this.server.close(); await once(this.server, 'close'); this.server = undefined; } } ``` -------------------------------- ### Create a new LoopBack 4 app Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/deployment/Deploying-with-pm2-and-nginx.md Use the LoopBack CLI to generate a new application. Refer to the getting started documentation for detailed instructions. ```sh $ lb4 app ``` -------------------------------- ### Generate Soap Calculator Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-next/examples/soap-calculator/README.md Use the LoopBack 4 CLI to generate the soap-calculator example project. This command clones the repository and sets up the example application. ```sh lb4 example soap-calculator ``` -------------------------------- ### Install @loopback/cli Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-next/packages/cli/README.md Run this command to install the CLI globally on your system. ```bash $ npm install -g @loopback/cli ``` -------------------------------- ### Clone and Test KeyValue-memory Connector Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-kv-connectors.md Instructions to clone the repository, navigate to the KeyValue-memory connector example for LoopBack 2.x, install dependencies, and run tests. ```shell git clone https://github.com/strongloop/loopback-example-kv-connectors.git cd loopback-example-kv-connectors/kv-memory-lb2x/ npm install npm test ``` -------------------------------- ### Bridge Connection Example Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/strong-pubsub.md Illustrates connecting a Client to a Bridge, and then the Bridge to a broker using another Client. This setup is useful for abstracting broker connections. ```javascript server.on('connection', function(connection) { var bridge = new Bridge( new MqttConnection(connection), new Client({port: MOSQUITTO_PORT}, Adapter) ); bridge.connect(); }); ``` -------------------------------- ### withExample Method Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/apidocs/openapi-spec-builder.componentsspecbuilder.md Defines a component example. ```APIDOC ## Method: withExample(name, example) ### Description Define a component example. ### Signature ```typescript withExample(name: string, example: ExampleObject): this ``` ``` -------------------------------- ### Accessing Data Source in Application Code Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-connector-mssql.md Refer to a configured data source in your application code. This example shows how to get the 'accountDB' data source object. ```javascript var app = require('./app'); var dataSource = app.dataSources.accountDB; ``` -------------------------------- ### Create a LoopBack Application Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/readmes/loopback-example-database.md Use the LoopBack CLI to create a new empty server application named 'loopback-example-database'. ```bash lb app loopback-example-database _-----_ | | ╭──────────────────────────╮ |--(o)--| │ Let's create a LoopBack │ `---------´ │ application! │ ( _´U`_ ) ╰──────────────────────────╯ /___A___ | ~ | __'.___.'__ ´ ` |° ´ Y ` ? What's the name of your application? loopback-example-database ? Enter name of the directory to contain the project: loopback-example-database info change the working directory to loopback-example-database ? Which version of LoopBack would you like to use? 3.x (current) ? What kind of application do you have in mind? empty-server (An empty LoopBack API, without any c onfigured models or datasources) ``` -------------------------------- ### URL-encoded query parameter for filtering Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/Parsing-requests.md Shows an example of a URL-encoded query parameter used to filter results for the GET /todos endpoint, specifically including a relation. ```http http://localhost:3000/todos?filter=%7B%22include%22%3A%5B%7B%22relation%22%3A%22todoList%22%7D%5D%7D ``` -------------------------------- ### Project Configuration Prompts Source: https://github.com/loopbackio/loopback.io/blob/gh-pages/pages/en/lb4/Getting-started.md Example prompts for configuring a new LoopBack 4 project, including name, description, and feature selection. ```sh ? 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: add a linter with pre-configured lint rules ◉ Enable prettier: install prettier to format code conforming to rules ◉ Enable mocha: install mocha to run tests ◉ Enable loopbackBuild: use @loopback/build helpers (e.g. lb-eslint) ◉ Enable editorconfig: add EditorConfig files ◉ Enable vscode: add VSCode config files ◉ Enable docker: include Dockerfile and .dockerignore ◉ Enable repositories: include repository imports and RepositoryMixin ◉ Enable services: include service-proxy imports and ServiceMixin ```