Configuring and Running a LoopBack Project in ServBay
What is LoopBack?
LoopBack is a highly extensible open-source Node.js framework developed by IBM, designed for rapidly building APIs and microservices. It uses a Model-Driven Development (MDD) approach, enabling developers to efficiently define data models and automatically generate powerful RESTful APIs. LoopBack comes with a rich set of features, including datasource connectors, user authentication and authorization, API explorers, and supports multiple databases and protocols.
Key Features and Benefits of LoopBack
- Model-Driven Development (MDD): Define data models in an intuitive way and the framework automatically generates API endpoints and data validation logic.
- Multi-datasource Support: Easily connect to a variety of popular databases (such as MongoDB, MySQL, PostgreSQL, MariaDB, etc.) as well as RESTful services and SOAP services.
- Powerful CLI Tools: LoopBack CLI (Command Line Interface) offers commands that greatly simplify project initialization, model definition, datasource configuration, and more.
- Modularity and Extensibility: The flexible framework design supports integration of third-party libraries and services via components and extensions to meet specific business needs.
- Built-in Security: Out-of-the-box authentication and authorization mechanisms help developers quickly build secure APIs.
With LoopBack, developers can significantly boost efficiency when building web application backends and APIs, creating high-performance and easy-to-maintain applications.
Setting Up and Running a LoopBack Project with ServBay
This guide will walk you through how to use the Node.js environment provided by ServBay to create and run a LoopBack project. We’ll leverage ServBay’s "website" feature to configure a local web server and use reverse proxying to forward external requests to the port where your LoopBack app is running—making local development and debugging convenient.
Creating a LoopBack Project
Install the LoopBack CLI
Before starting, make sure you have installed and enabled the Node.js package in ServBay. Then, open your terminal and use npm to globally install the LoopBack CLI:
bashnpm install -g @loopback/cli
1Initialize the Project
Navigate to ServBay’s recommended website root directory
/Applications/ServBay/www
, and use the LoopBack CLI to initialize a new application project. It’s best to use lowercase letters and hyphens for your project name, e.g.,servbay-loopback-app
:bashcd /Applications/ServBay/www lb4 app servbay-loopback-app
1
2The CLI will guide you through project setup, including selecting features (like REST API, serialization, authentication, etc.) and setting up the project structure. Choose the options that fit your needs.
Install Project Dependencies
Go into your newly created project folder and install the required Node.js dependencies:
bashcd servbay-loopback-app npm install
1
2
Modifying the LoopBack Project Output (Optional)
To demonstrate how to access your app via ServBay, you can modify the default /ping
endpoint so that it returns a custom response.
Edit the
src/controllers/ping.controller.ts
FileLocate the
src/controllers/ping.controller.ts
file in your project. Update its content as shown below so the/ping
endpoint returns the string "Hello ServBay!":typescriptimport {get} from '@loopback/rest'; export class PingController { constructor() {} @get('/ping') ping(): string { // Change the return value to "Hello ServBay!" return 'Hello ServBay!'; } }
1
2
3
4
5
6
7
8
9
10
11
Running the Project in Development Mode
During development, you'll typically run the LoopBack app's built-in development server for quick iteration and debugging. We'll access this development server through ServBay's reverse proxy.
Run the Development Server
In your project root, use the terminal to start your LoopBack app. LoopBack apps usually listen on a specified port. You can set the
PORT
environment variable, for example, to use port8585
:bashcd /Applications/ServBay/www/servbay-loopback-app PORT=8585 npm start
1
2After the app starts, you should see output like "Server is running at http://[::1]:8585" in the terminal.
Configure the ServBay Website (Reverse Proxy)
Open the ServBay Control Panel and navigate to the "Website" section. Click the add button to create a new reverse proxy website, forwarding requests on an external domain and port to your LoopBack app’s internal address and port:
- Name: For example,
My first LoopBack dev site
(for identification in ServBay) - Domain: For example,
servbay-loopback-dev.servbay.demo
(the local domain you’ll access in your browser) - Type: Select
Reverse Proxy
- IP Address: Enter
127.0.0.1
(your LoopBack app runs locally) - Port: Enter
8585
(the port your LoopBack app listens on)
Save the configuration and apply the changes in ServBay. ServBay will automatically configure local DNS (by updating the
/etc/hosts
file or using ServBay Helper Tool) and the web server (Caddy or Nginx), allowing you to access your app via the configured domain.For details on adding a reverse proxy website in ServBay, refer to: Add a Node.js Development Website.
- Name: For example,
Accessing the App in Development Mode
Open your web browser and navigate to your configured domain and the LoopBack endpoint, such as
https://servbay-loopback-dev.servbay.demo/ping
.Since ServBay supports automatic SSL certificate configuration for local sites (via ServBay User CA or ServBay Public CA), you can securely access your local development site directly via HTTPS. This simulates a production environment and helps you catch potential SSL issues early. For more on ServBay SSL features, see: Using SSL to Secure Your Website.
Deploying a Production Version (Optional)
When your LoopBack project is ready for deployment, you may want to run it in production mode. In production, the LoopBack app will perform better with less debug output.
Run the Production Server
In your project root, start the LoopBack app in production mode. Again, specify a port such as
8586
, and set theNODE_ENV
environment variable toproduction
:bashcd /Applications/ServBay/www/servbay-loopback-app PORT=8586 NODE_ENV=production npm start
1
2Configure the ServBay Website (Reverse Proxy)
Go back to the "Website" section in the ServBay Control Panel. Add or edit a reverse proxy website to point to your LoopBack app running in production mode:
- Name: For example,
My first LoopBack production site
- Domain: For example,
servbay-loopback-prod.servbay.demo
- Type: Select
Reverse Proxy
- IP Address: Enter
127.0.0.1
- Port: Enter
8586
(the port of your production app)
Save the settings and apply the changes.
- Name: For example,
Accessing the App in Production Mode
Open your browser and visit your production domain, e.g.,
https://servbay-loopback-prod.servbay.demo/ping
, to check the status of your production build. Once again, ServBay will provide HTTPS support automatically.
Connecting to Databases
The LoopBack framework supports a variety of databases via datasource connectors. ServBay offers MySQL, MariaDB, PostgreSQL, MongoDB, and Redis packages. You can easily enable these database services in ServBay and configure your LoopBack project to connect to them.
Below are sample configurations for connecting to several common databases provided by ServBay. Modify the configuration to suit your actual requirements.
Important: The database usernames and passwords in the following examples are placeholders. Be sure to use the actual root user password or other credentials you set in ServBay for the relevant database. Database users and passwords can be managed in the ServBay control panel.
Connecting to MongoDB
First, ensure you’ve installed and started the MongoDB package in ServBay. Install the LoopBack MongoDB connector:
bashnpm install --save @loopback/connector-mongodb
1Then configure the datasource in your project (usually in
src/datasources/mongodb.datasource.ts
or a similarly named file):typescript// src/datasources/mongodb.datasource.ts import {juggler} from '@loopback/repository'; const config = { name: 'mongodb', connector: 'mongodb', // ServBay MongoDB runs on localhost:27017 by default url: 'mongodb://localhost:27017/servbay-loopback-app', // Replace with your database name host: 'localhost', port: 27017, user: '', // Fill in if authentication is enabled password: '', // Fill in if authentication is enabled database: 'servbay-loopback-app', // Replace with your database name // Other optional settings... }; 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
20
21
22Connecting to Redis
Ensure you’ve installed and started the Redis package in ServBay. Install a Redis connector for LoopBack (Note: There is no official
@loopback/connector-redis
. Popular options include the community-providedloopback-connector-redis
or using Node.js’s nativeredis
library combined with LoopBack’s service concepts):bashnpm install --save loopback-connector-redis # or another compatible Redis library
1Then configure the datasource in your project (the example below uses the
loopback-connector-redis
approach):typescript// src/datasources/redis.datasource.ts import {juggler} from '@loopback/repository'; const config = { name: 'redis', connector: 'redis', // ServBay Redis runs on localhost:6379 by default url: 'redis://localhost:6379', // Other optional settings... }; 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
15
16
17Note: LoopBack 4 uses datasources differently compared to LoopBack 3. The above example is a simplified LoopBack 3 style. In LoopBack 4, it’s recommended to use the
@datasource
decorator from@loopback/repository
and modern configuration practices. Refer to the official LoopBack 4 docs for up-to-date datasource configuration guidance. Connection parameters likehost
,port
,user
,password
,database
are still generally applicable.Connecting to MariaDB (or MySQL)
Ensure you’ve installed and started the MariaDB or MySQL package in ServBay. ServBay supports both databases. The LoopBack MySQL connector typically works for MariaDB as well. Install the LoopBack MySQL connector:
bashnpm install --save @loopback/connector-mysql
1Then configure the datasource:
typescript// src/datasources/mariadb.datasource.ts or mysql.datasource.ts import {juggler} from '@loopback/repository'; const config = { name: 'mariadb', // or 'mysql' connector: 'mysql', // use the MySQL connector // url: '', // You can use a url or configure host, port, database separately host: 'localhost', // ServBay DBs run on localhost by default port: 3306, // Default MariaDB/MySQL port user: 'root', // Default user is usually root in ServBay password: 'password', // << Replace with the actual database password from ServBay >> database: 'servbay_loopback_app', // Replace with your database name // Other optional settings... }; export class MariaDbDataSource extends juggler.DataSource { static dataSourceName = 'mariadb'; // or 'mysql' constructor() { super(config); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21Notice: The
password: 'password'
above is a placeholder. Be sure to look up or set your MariaDB/MySQL root user password in the ServBay Control Panel and use it in your configuration.Connecting to PostgreSQL
Ensure the PostgreSQL package is installed and running in ServBay. Install the LoopBack PostgreSQL connector:
bashnpm install --save @loopback/connector-postgresql
1Then configure the datasource:
typescript// src/datasources/postgresql.datasource.ts import {juggler} from '@loopback/repository'; const config = { name: 'postgresql', connector: 'postgresql', // url: '', // You can use a url or configure host, port, database separately host: 'localhost', // ServBay PostgreSQL runs on localhost by default port: 5432, // Default PostgreSQL port user: 'user', // << Replace with your PostgreSQL username from ServBay >> password: 'password', // << Replace with your actual password from ServBay >> database: 'servbay_loopback_app', // Replace with your database name // Other optional settings... }; 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
21Note: The
user: 'user'
andpassword: 'password'
above are placeholders. Be sure to create or find your PostgreSQL user and password in the ServBay Control Panel and enter them in the configuration.
Conclusion
By following the steps above, you have successfully created, configured, and run a LoopBack Node.js project in the ServBay local development environment. You’ve learned how to use ServBay’s Website feature to access your app via reverse proxy, and how to connect your LoopBack project to various database services provided by ServBay.
ServBay provides a stable and convenient local development environment for LoopBack and other Node.js frameworks, integrating web servers, databases, and multiple language runtimes—all of which greatly simplifies developer workflows. By leveraging ServBay’s website management, SSL support, and database integration, you can develop and test locally more efficiently and securely.