LoopBack is a highly-extensible, open-source Node.js framework that enables developers to create dynamic end-to-end REST APIs with minimal coding.
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
LoopBack application creation and management functionality for creating Express-based applications with model management, middleware configuration, and data source integration.
Creates a new LoopBack application instance that extends Express with LoopBack-specific functionality.
/**
* Create a LoopBack application instance
* @param {Object} options - Optional configuration options
* @param {boolean} options.localRegistry - Use local registry instead of global
* @param {boolean} options.loadBuiltinModels - Load built-in models when using local registry
* @returns {LoopBackApplication} Express-based LoopBack application
*/
function loopback(options);Usage Example:
const loopback = require('loopback');
// Basic application
const app = loopback();
// Application with local registry
const isolatedApp = loopback({
localRegistry: true,
loadBuiltinModels: true
});Attach models to the application and configure their settings.
/**
* Attach a model to the application
* @param {Function} Model - Model constructor to attach
* @param {Object} config - Optional model configuration
* @param {string} config.dataSource - Data source name to attach model to
* @param {boolean} config.public - Whether model should be exposed via REST API
* @param {Object} config.options - Additional model options
*/
app.model(Model, config);
/**
* Remove a model from the application by name
* @param {string} modelName - Name of model to remove
*/
app.deleteModelByName(modelName);
/**
* Get all models attached to the application
* @returns {Function[]} Array of model constructors
*/
app.models();Usage Examples:
const Book = loopback.createModel('Book', {
title: String,
author: String
});
// Attach model with configuration
app.model(Book, {
dataSource: 'db',
public: true
});
// Get all attached models
const attachedModels = app.models();Configure and manage data sources for the application.
/**
* Define a data source for the application
* @param {string} name - Data source name
* @param {Object} config - Data source configuration
* @param {string} config.connector - Connector name or constructor
* @param {string} config.host - Database host (if applicable)
* @param {number} config.port - Database port (if applicable)
* @param {string} config.database - Database name (if applicable)
* @param {string} config.username - Database username (if applicable)
* @param {string} config.password - Database password (if applicable)
* @returns {DataSource} Created data source instance
*/
app.dataSource(name, config);
/**
* Register a connector with the application
* @param {string} name - Connector name
* @param {Function} connector - Connector constructor
*/
app.connector(name, connector);Usage Examples:
// Define a database connection
app.dataSource('db', {
connector: 'mysql',
host: 'localhost',
port: 3306,
database: 'myapp',
username: 'dbuser',
password: 'dbpass'
});
// Define memory data source
app.dataSource('memory', {
connector: 'memory'
});
// Register custom connector
app.connector('custom', MyCustomConnector);Enable and configure authentication for the application.
/**
* Enable authentication for the application
* @param {Object} options - Authentication configuration options
* @param {Object} options.dataSource - Data source for auth models
* @param {Object} options.userModel - User model configuration
* @param {Object} options.userIdentityModel - UserIdentity model configuration
* @param {Object} options.userCredentialModel - UserCredential model configuration
*/
app.enableAuth(options);Usage Example:
// Enable basic authentication
app.enableAuth({
dataSource: 'db',
userModel: 'User'
});Configure middleware phases and register middleware functions.
/**
* Define middleware phases for the application
* @param {string|string[]} nameOrArray - Phase name or array of phase names
*/
app.defineMiddlewarePhases(nameOrArray);
/**
* Register middleware in a specific phase
* @param {string} phase - Middleware phase name
* @param {string|string[]} paths - Optional paths to apply middleware to
* @param {Function} handler - Middleware function
*/
app.middleware(phase, paths, handler);
/**
* Register middleware from configuration
* @param {Function} factory - Middleware factory function
* @param {Object} config - Middleware configuration
*/
app.middlewareFromConfig(factory, config);Built-in Middleware Phases:
initial - Initialization phasesession - Session handlingauth - Authenticationparse - Request parsingroutes - Route handlingfiles - Static file servingfinal - Final cleanupUsage Examples:
// Define custom middleware phases
app.defineMiddlewarePhases(['validation', 'transform']);
// Register middleware in phases
app.middleware('initial', (req, res, next) => {
console.log('Request received:', req.method, req.url);
next();
});
app.middleware('auth', '/api', loopback.token());Access and configure remote object exposure for REST APIs.
/**
* Get the RemoteObjects instance for the application
* @returns {RemoteObjects} Strong-remoting RemoteObjects instance
*/
app.remotes();
/**
* Get remote objects as a plain object
* @returns {Object} Plain object representation of remote objects
*/
app.remoteObjects();
/**
* Get handler for specific type
* @param {string} type - Handler type (e.g., 'rest', 'json')
* @param {Object} options - Handler options
* @returns {Function} Handler function
*/
app.handler(type, options);Additional Express-related functionality specific to LoopBack applications.
/**
* Remove a route by reference (opposite of app.use)
* @param {*} route - Route reference to remove
*/
app.disuse(route);
/**
* Enhanced listen method with port configuration
* @param {Function} cb - Optional callback function
* @returns {http.Server} HTTP server instance
*/
app.listen(cb);Usage Example:
// Start server with callback
app.listen(() => {
console.log('LoopBack server is running on port', app.get('port') || 3000);
});/**
* Application instance properties
*/
interface LoopBackApplication {
// Registry and data management
registry: Registry; // Model registry instance
models: Object; // Attached models object
datasources: Object; // Data sources registry
dataSources: Object; // Alias for datasources
connectors: Object; // Registered connectors
// Framework reference
loopback: LoopBackStatic; // Reference to main loopback object
// Express application properties inherited
// ... all Express application properties
}