@axonlabs/core - v0.13.1
    Preparing search index...

    Class AxonCore

    Index

    Constructors

    Methods

    • Clears all the global middlewares

      Returns void

    • Closes the web servers

      Parameters

      • Optionalserver: "http" | "https"

        server to close

      Returns boolean

      core.close() // closes both http and https servers
      core.close("http") // closes only http server
      core.close("https") // closes only https server
    • Returns the server object

      Returns {
          http: null | Server<typeof IncomingMessage, typeof ServerResponse>;
          https: null | Server<typeof IncomingMessage, typeof ServerResponse>;
      }

      server object

      const servers = core.getServers();
      servers.http.on("request", () => {
      // something here
      });
    • You can set one or many middlewares in global scope with this method.

      Parameters

      • fn: Middleware | Middleware[]

        middleware function or array of middleware functions

      • Optionaltimeout: number

        timeout in milliseconds

      • critical: boolean = false

        whether the middleware is critical (defaults to false)

      Returns Promise<void>

      core.globalMiddleware(authMiddleware, 5000, true) // critical middleware with 5s timeout
      core.globalMiddleware([uploadMiddleware, userMiddleware], 10000, false) // optional middlewares with 10s timeout
    • Start listening to http incoming requests

      Parameters

      • host: string = '127.0.0.1'

        server host address

      • port: number | { http: number; https: number } = 8000

        server port

      • Optionalcallback: (mode?: string) => void

        callback a function to run after starting to listen

      Returns void

      core.listen("0.0.0.0", 80)
      // or
      core.listen("0.0.0.0", {
      https: 443,
      http: 80
      })
    • Loads a specified Axon plugin using the plugin loader.

      Parameters

      • plugin: AxonPlugin

        The plugin to be loaded. It should be an instance of AxonPlugin.

      Returns Promise<void>

      // this class must implements AxonPlugin type
      class MyPlugin implements AxonPlugin {
      name = "plugin"
      version = "1.0.0"

      init(core) {}
      }

      core.loadPlugin(new MyPlugin())
    • loads created routes

      Parameters

      • router: AxonRouter

        instance of Router which routes set with it.

      Returns Promise<void>

      const router = Router(); // without prefix
      const router2 = Router("/api/v1"); // with prefix

      router.get('/', async (req, res) => {});

      core.loadRoute(router);
    • Register a factory function that creates the dependency (sync or async).

      Default lifecycle of dependencies is singleton

      Type Parameters

      • T

      Parameters

      • name: string | string[]

        String, array of string or Function key for the dependency

      • factory: () => T | Promise<T>

        Factory function or something that must run to return the instance of dependency

      • Optionaloptions: { lifecycle?: Lifecycle }

        Some options for configuring the dependency

      Returns void

      0.13.0

      core.registerDependencyFactory('db', () => new DB())
      core.registerDependencyFactory('auth', async () => await AuthService.build())
    • Register a static value or instance (object, function, class instance).

      Default lifecycle of dependencies is singleton

      Type Parameters

      • T

      Parameters

      • name: string | string[]

        String, array of string or Function key for the dependency

      • value: T

        Function, object or instance to register as dependency value

      • Optionaloptions: { lifecycle?: Lifecycle }

        Some options for configuring the dependency

      Returns void

      0.13.0

      core.registerDependencyValue('logger', new Logger());
      core.registerDependencyValue('config', { port: 3000 });
    • unload routes based on entered parameters

      Parameters

      Returns Promise<void>

      // this one unloads a route with path `/api/v1/user`.
      core.unloadRoute({
      route: '/api/v1/user'
      });

      // this one unloads all routes with method `GET`
      core.unloadRoute({
      method: 'GET'
      });

      const userRouter = Router();

      // this one unloads all routes of userRouter.
      core.unloadRoute({
      router: userRouter
      });

      // this one unloads a route with path `/api/v1/user`, all routes with method `GET` and all routes of userRouter.
      core.unloadRoute({
      route: '/api/v1/user',
      method: "GET",
      router: userRouter
      })
    • unload all routes

      Returns Promise<void>

      core.unloadRoutes();