What is @backstage/backend-common?
@backstage/backend-common is a utility library for building backend services in the Backstage ecosystem. It provides common functionalities such as logging, database connections, and server setup, which are essential for developing robust and scalable backend services.
What are @backstage/backend-common's main functionalities?
Logging
Provides a root logger that can be used throughout the application for consistent logging.
const { getRootLogger } = require('@backstage/backend-common');
const logger = getRootLogger();
logger.info('This is an info message');
Database Connection
Helps in setting up and managing database connections using a configuration object.
const { DatabaseManager } = require('@backstage/backend-common');
const config = { /* database configuration */ };
const databaseManager = DatabaseManager.fromConfig(config);
const db = databaseManager.forPlugin('my-plugin');
Server Setup
Simplifies the process of setting up a web server with routing and middleware.
const { createServiceBuilder } = require('@backstage/backend-common');
const service = createServiceBuilder(module)
.setPort(7000)
.addRouter('/api', myApiRouter)
.start();
Other packages similar to @backstage/backend-common
express
Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications. Unlike @backstage/backend-common, which is tailored for the Backstage ecosystem, Express is a general-purpose web framework.
winston
Winston is a versatile logging library for Node.js. While @backstage/backend-common includes logging as one of its features, Winston is a dedicated logging library with more advanced features and customization options.
knex
Knex is a SQL query builder for Node.js, designed to be flexible and portable. It provides a similar database management functionality as @backstage/backend-common but is more focused on query building and database migrations.
@backstage/backend-common
[!CAUTION]
This package is deprecated and will be removed in a near future, so please follow the deprecated instructions for the exports you still use.
Common functionality library for Backstage backends, implementing logging,
error handling and similar.
Usage
Add the library to your backend package:
yarn --cwd packages/backend add @backstage/backend-common
then make use of the handlers and logger as necessary:
import {
errorHandler,
getRootLogger,
notFoundHandler,
requestLoggingHandler,
} from '@backstage/backend-common';
const app = express();
app.use(requestLoggingHandler());
app.use('/home', myHomeRouter);
app.use(notFoundHandler());
app.use(errorHandler());
app.listen(PORT, () => {
getRootLogger().info(`Listening on port ${PORT}`);
});
Documentation