Your first module

Create your first module

Your first app works without a module. Create one when a feature grows and you want to keep its code in one place. For this example, a module is just one new file.

Create the folder

text
module/
  acme.hello/
    plugin.ts

The folder name becomes the module name. acme is an example of a vendor name; use your own name to avoid collisions.

Export an action

In module/acme.hello/plugin.ts:

ts
import { Access } from "@qino/qino";

export const api = {
  ping: {
    get: {
      description: "Reply to a ping",
      access: Access.PUBLIC,
      execute: () => ({ pong: true }),
    },
  },
};

This gives your app one new URL: GET /api/acme.hello/ping.

Register the module

In your application's server.ts, add the plugin before app.init():

ts
import { App, sys } from "@qino/qino";

const app = new App();
app.modules.add(import.meta.resolve("./module/acme.hello/plugin.ts"));

await app.init();
sys.serve({ port: 8080 }, app.fetch);

Start the server as before, then open localhost:8080/api/acme.hello/ping in your browser. You should see {"pong":true}. The call to app.modules.add() is the only change in server.ts.

Later, when you need more

For this example, you don't need a manifest or a store. If your module later depends on other modules, add a manifest.json. The module guide explains that and other lifecycle features. Stores become useful when you want to offer a collection of modules.