backend-defaults: pretty-print JSON responses during development
Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
---
|
||||
'@backstage/backend-defaults': patch
|
||||
---
|
||||
|
||||
The default root HTTP service implementation will now pretty-print JSON responses in development.
|
||||
|
||||
If you are overriding the `rootHttpRouterServiceFactory` with a `configure` function that doesn't call `applyDefaults`, you can introduce this functionality by adding the following snippet inside `configure`:
|
||||
|
||||
```ts
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
app.set('json spaces', 2);
|
||||
}
|
||||
```
|
||||
@@ -59,7 +59,7 @@ const backend = createBackend();
|
||||
|
||||
backend.add(
|
||||
rootHttpRouterServiceFactory({
|
||||
configure: ({ app, middleware, routes, config, logger, lifecycle }) => {
|
||||
configure: ({ app, middleware, routes, config, logger, healthRouter }) => {
|
||||
// Refer to https://expressjs.com/en/guide/writing-middleware.html on how to write express middleware
|
||||
const customMiddleware = {
|
||||
logging(): RequestHandler {
|
||||
@@ -88,11 +88,18 @@ backend.add(
|
||||
},
|
||||
};
|
||||
|
||||
// The default implementation pretty-prints JSON responses in development
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
app.set('json spaces', 2);
|
||||
}
|
||||
|
||||
// the built in middleware is provided through an option in the configure function
|
||||
app.use(middleware.helmet());
|
||||
app.use(middleware.cors());
|
||||
app.use(middleware.compression());
|
||||
|
||||
app.use(healthRouter);
|
||||
|
||||
// you can add you your own middleware in here
|
||||
app.use(customMiddleware.logging());
|
||||
|
||||
|
||||
+3
@@ -106,6 +106,9 @@ const rootHttpRouterServiceFactoryWithOptions = (
|
||||
lifecycle,
|
||||
healthRouter,
|
||||
applyDefaults() {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
app.set('json spaces', 2);
|
||||
}
|
||||
app.use(middleware.helmet());
|
||||
app.use(middleware.cors());
|
||||
app.use(middleware.compression());
|
||||
|
||||
Reference in New Issue
Block a user