Create and Run a LoopBack Project
What is LoopBack?
LoopBack is a highly scalable Node.js framework developed by IBM for building APIs and microservices. It provides powerful tools and features that make it easy for developers to create, test, and deploy APIs. LoopBack utilizes model-driven development (MDD) to quickly generate RESTful APIs and supports various database connections.
Main Features and Advantages of LoopBack
- Model-Driven Development: Automatically generates RESTful APIs by defining models.
- Multiple Database Support: Supports various databases such as MongoDB, MySQL, PostgreSQL, MariaDB, etc.
- Powerful CLI Tools: LoopBack CLI offers a wide range of commands that simplify project creation and management.
- Rich Plugins and Extensions: Comes with a rich set of plugins and extensions to easily integrate third-party services and libraries.
- Built-in User Authentication and Authorization: Provides a robust mechanism for user authentication and authorization.
Using LoopBack can help developers quickly build high-performance and scalable web applications and APIs.
Create and Run a LoopBack Project with ServBay
In this article, we will create and run a LoopBack project using the Node.js environment provided by ServBay. We will utilize the 'Host' feature of ServBay to set up a web server and use reverse proxy for project access.
Create a LoopBack Project
Install LoopBack CLI
First, ensure that you have installed the Node.js environment provided by ServBay. Then, use the following command to globally install the LoopBack CLI:
bashnpm install -g @loopback/cli
1Initialize Project
Create a new LoopBack project in the recommended root directory
/Applications/ServBay/www
:bashcd /Applications/ServBay/www lb4 app servbay-loopback-app
1
2Follow the prompts to enter the project name (it is recommended to name it
servbay-loopback-app
) and select other options as needed.Install Dependencies
Navigate to the project directory and install dependencies:
bashcd servbay-loopback-app npm install
1
2
Modify LoopBack Project Output
Modify
src/controllers/ping.controller.ts
fileOpen the
src/controllers/ping.controller.ts
file and change the content to display "Hello ServBay!" on the webpage:typescriptimport {get} from '@loopback/rest'; export class PingController { constructor() {} @get('/ping') ping(): string { return 'Hello ServBay!'; } }
1
2
3
4
5
6
7
8
9
10
Enter Development Mode
Run Development Server
Start the development server and specify the port (e.g., 8585):
bashPORT=8585 npm start
1This will start a local development server and expose port 8585.
Configure ServBay Host Reverse Proxy
Use the 'Host' feature of ServBay to access the development server via reverse proxy. In the ServBay 'Host' settings, add a new reverse proxy:
- Name:
My first LoopBack dev site
- Domain:
servbay-loopback-test.dev
- Host Type:
Reverse Proxy
- IP:
127.0.0.1
- Port:
8585
For detailed setup steps, refer to adding a Node.js development website.
- Name:
Access Development Mode
Open your browser and visit
https://servbay-loopback-test.dev/ping
to view the project in real-time. Since ServBay supports custom domains and free SSL certificates, you will enjoy enhanced security.
Deploy Production Version
Prepare Production Environment
Ensure that your project can run properly in a production environment. Typically, LoopBack projects do not require special build steps, but you may need to set environment variables or other configurations.
Run Production Server
Start the production server and specify the port (e.g., 8586):
bashPORT=8586 NODE_ENV=production npm start
1Configure ServBay Host Reverse Proxy
Use the 'Host' feature of ServBay to access the production server via reverse proxy. In the ServBay 'Host' settings, add a new reverse proxy:
- Name:
My first LoopBack production site
- Domain:
servbay-loopback-test.prod
- Host Type:
Reverse Proxy
- IP:
127.0.0.1
- Port:
8586
- Name:
Access Production Mode
Open your browser and visit
https://servbay-loopback-test.prod
to view the production version. With ServBay’s custom domain and free SSL certificate feature, your website will have enhanced security and credibility.
Database Connection
ServBay offers support for Redis, MariaDB, PostgreSQL, and MongoDB databases. Below are examples of how to connect to these databases.
Connect to MongoDB
Install
@loopback/connector-mongodb
:bashnpm install --save @loopback/connector-mongodb
1Then configure the data source in the project:
typescript// src/datasources/mongodb.datasource.ts import {juggler} from '@loopback/repository'; const config = { name: 'mongodb', connector: 'mongodb', url: 'mongodb://localhost/servbay-loopback-app', host: 'localhost', port: 27017, user: '', password: '', database: 'servbay-loopback-app', }; export class MongodbDataSource extends juggler.DataSource { static dataSourceName = 'mongodb'; constructor() { super(config); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20Connect to Redis
Install
loopback-connector-redis
:bashnpm install --save loopback-connector-redis
1Then configure the data source in the project:
typescript// src/datasources/redis.datasource.ts import {juggler} from '@loopback/repository'; const config = { name: 'redis', connector: 'redis', url: 'redis://localhost:6379', }; export class RedisDataSource extends juggler.DataSource { static dataSourceName = 'redis'; constructor() { super(config); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15Connect to MariaDB
Install
@loopback/connector-mysql
:bashnpm install --save @loopback/connector-mysql
1Then configure the data source in the project:
typescript// src/datasources/mariadb.datasource.ts import {juggler} from '@loopback/repository'; const config = { name: 'mariadb', connector: 'mysql', url: '', host: 'localhost', port: 3306, user: 'root', password: 'password', database: 'servbay_loopback_app', }; export class MariaDbDataSource extends juggler.DataSource { static dataSourceName = 'mariadb'; constructor() { super(config); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20Connect to PostgreSQL
Install
@loopback/connector-postgresql
:bashnpm install --save @loopback/connector-postgresql
1Then configure the data source in the project:
typescript// src/datasources/postgresql.datasource.ts import {juggler} from '@loopback/repository'; const config = { name: 'postgresql', connector: 'postgresql', url: '', host: 'localhost', port: 5432, user: 'user', password: 'password', database: 'servbay_loopback_app', }; export class PostgresqlDataSource extends juggler.DataSource { static dataSourceName = 'postgresql'; constructor() { super(config); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
By following the above steps, you have successfully created and run a LoopBack project, utilized the features provided by ServBay to manage and access your project, and connected to various databases.