Page Contents
Prerequisites
Install the following:
- Node.js version 7 or higher.
-
TypeScript version 2 or higher:
npm i -g typescript
-
TypeScript Node version 3 or higher:
npm i -g ts-node
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
Important:
You must set 'target': 'es6'
in your compiler options in your tsconfig.json
.
{
"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:
-
Install ts-node:
npm install -g ts-node
-
Run the app:
ts-node index.ts
You should see “world” written to the console.
OR:
-
Install TypeScript >= 2.0.0
npm i -g typescript
-
Compile
index.ts
by entering this command:tsc index.ts
-
Run the compiled JavaScript output file by entering this command:
node index.js
You should see “world” written to the console.