I'm following this example here for a simple logging middleware setup. But I can't seem to get it to log my request information. I get the standard response when I hit /ping, but nothing is logged out to the console.
I try to register the middleware in two different locations. index.ts and application.ts.
// src/middleware/log.middleware.ts
import { Next } from '@loopback/core'
import { Middleware, MiddlewareContext } from '@loopback/rest'
export const testLoggingMiddleware: Middleware = async (
middlewareCtx: MiddlewareContext,
next: Next,
) => {
const { request } = middlewareCtx
console.log('Request: %s %s', request.method, request.originalUrl)
try {
// Proceed with next middleware
await next()
// Process response
console.log('Response received for %s %s', request.method, request.originalUrl)
} catch (err) {
// Catch errors from downstream middleware
console.error('Error received for %s %s', request.method, request.originalUrl)
throw err
}
}
// src/index.ts
import { ApplicationConfig, RemoteConfigurationsApplication } from './application'
import { testLoggingMiddleware } from './middleware/log.middleware'
import { NodeEnvironmentService } from './services'
export * from './application'
export async function main(
options: ApplicationConfig = {},
): Promise<RemoteConfigurationsApplication> {
const app = new RemoteConfigurationsApplication(options)
app.middleware(testLoggingMiddleware) // trying to add the middleware here as well
await app.boot()
await app.start()
const url = app.restServer.url
console.log(`Server is running at ${url}`)
console.log(`Try ${url}/ping`)
return app
}
if (require.main === module) {
// Run the application
const config = {
rest: {
port: +(process.env.PORT ?? 8081),
host: process.env.HOST,
// The `gracePeriodForClose` provides a graceful close for http/https
// servers with keep-alive clients. The default value is `Infinity`
// (don't force-close). If you want to immediately destroy all sockets
// upon stop, set its value to `0`.
// See https://www.npmjs.com/package/stoppable
gracePeriodForClose: 5000, // 5 seconds
openApiSpec: {
// useful when used with OpenAPI-to-GraphQL to locate your application
setServersFromRequest: true,
},
basePath: `/`,
cors: {
origin: '*',
methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
preflightContinue: false,
optionsSuccessStatus: 204,
credentials: true,
},
},
}
main(config).catch((err) => {
console.error('Cannot start the application.', err)
process.exit(1)
})
}
// src/application.ts
require('dotenv').config()
import { BootMixin } from '@loopback/boot'
import { ApplicationConfig } from '@loopback/core'
import { HealthComponent } from '@loopback/extension-health'
import { RestExplorerBindings, RestExplorerComponent } from '@loopback/rest-explorer'
import { RepositoryMixin } from '@loopback/repository'
import { RestApplication } from '@loopback/rest'
import { ServiceMixin } from '@loopback/service-proxy'
import * as path from 'path'
import { testLoggingMiddleware } from './middleware/log.middleware'
import { MySequence } from './sequence'
export { ApplicationConfig }
export class RemoteConfigurationsApplication extends BootMixin(
ServiceMixin(RepositoryMixin(RestApplication)),
) {
constructor(options: ApplicationConfig = {}) {
super(options)
// Set up the custom sequence
this.sequence(MySequence)
// Set up default home page
this.static('/', path.join(__dirname, '../public'))
// Customize @loopback/http-explorer configuration here
this.configure(RestExplorerBindings.COMPONENT).to({
path: '/explorer',
})
this.component(RestExplorerComponent)
this.component(HealthComponent)
this.middleware(testLoggingMiddleware) // adding middleware in application
this.projectRoot = __dirname
// Customize @loopback/boot Booter Conventions here
this.bootOptions = {
controllers: {
// Customize ControllerBooter Conventions here
dirs: ['controllers'],
extensions: ['.controller.js'],
nested: true,
},
}
}
}