Search code examples
nuxt.jsnuxtjs3

How to retrieve server side plugin


I wrote a plugin to use on the server side and defined it as follows:


    export default defineNuxtPlugin((nuxtApp:any) => {
      const kafkaOptions: any = nuxtApp.$config.public.kafkaModule.kafkaOptions;
      const kafkaInstance = createKafkaInstance(kafkaOptions);
      return {
        provide: {
          kafkaFactory: kafkaInstance,
        },
      };
    });


    import { useNuxtApp } from "#app"
    import { addServerPlugin, createResolver, defineNuxtModule, useLogger } from "@nuxt/kit"
    export default defineNuxtModule({
        meta: {
          name: '@pf/kafka',
          configKey: 'kafkaModule',
        },
       
        setup(moduleOptions, nuxt) {
          const logger = useLogger();
          logger.log(moduleOptions);
          const { resolve } = createResolver(import.meta.url);
          addServerPlugin(resolve('./runtime/kafka-server.ts'))
      
        },
      });   

This builds fine, and the dist folder is generated. However, I have no idea how to access the plugin on the server side. Is this the correct way of doing it? I want to create a single instance of kafka and the nitro plugin does not seem the best way to accomplish this. If this is a suitable way how can I access the plugin in a request handler defineEventHandler?


Solution

  • I created a helper is server/services/helper as a composable and used that instead