Page Contents

Prerequisites

Install the following:

Install LoopBack core

Then add LoopBack 4 as a dependency to your Node.js project:

npm install -s @loopback/core

Create a Hello World project

With LoopBack 4 you can code in JavaScript or TypeScript.

JavaScript project

Create index.js:

const Application = require('@loopback/core').Application;

const app = new Application();
app.bind('hello').to('world');
app.get('hello').then(value => {
  console.log(value);
});

Then run index.js:

node index.js

You should see “world” written to the console.

TypeScript project

{
  "compilerOptions": {
    // ...
    "target": "es2017" //<-- Add this
  }
}

Create index.ts:

import {Application} from '@loopback/core';

const app = new Application();
app.bind('hello').to('world');
app.get('hello').then(value => {
  console.log(value);
});

Then run index.ts. Do one of the following:

  1. Install ts-node:

     npm install -g ts-node
    
  2. Run the app:

     ts-node index.ts
    

You should see “world” written to the console.

OR:

  1. Install TypeScript >= 2.0.0

     npm i -g typescript
    
  2. Compile index.ts by entering this command:

     tsc index.ts
    
  3. Run the compiled JavaScript output file by entering this command:

    node index.js
    

    You should see “world” written to the console.