@axiosleo/koapp

A framework designed for rapid web application development with Node.js
Built on Koa
npm install @axiosleo/koapp
Initialization
npx @axiosleo/koapp init <app-name> -d <optional-dir>
Quick Start
const { KoaApplication, Router, success } = require("@axiosleo/koapp");
const handle = async (ctx) => {
success({
message: "Hello World!",
});
};
const router = new Router("/test", {
method: "any",
handlers: [handle],
});
const app = new KoaApplication({
port: 8088,
listen_host: "localhost",
routers: [router],
});
app.start();
More Examples
See validatorjs for more rule examples
See Router examples for more usage: tests/bootstrap.js
const { Router } = require("@axiosleo/koapp");
const router = new Router("/test", {
method: "any",
validator: {
params: {
id: "required|integer",
},
query: {
name: "required|string",
},
body: {
age: "required|integer",
},
},
handlers: [],
});
const multer = require("@koa/multer");
root.post("/upload", async (context) => {
const upload = multer();
const func = upload.any();
await func(context.koa, async () => {});
const file = context.koa.request.files[0];
context.koa.set("content-type", file.mimetype);
context.koa.body = file.buffer;
context.koa.attachment(file.originalname);
});
const { _foreach, _sleep } = require("@axiosleo/cli-tool/src/helper/cmd");
const test = async (context) => {
await _foreach(["0", "1", "2", "3"], async (item, index) => {
context.koa.sse.send({ data: { item, index } });
await _sleep(1000);
});
context.koa.sse.end();
};
const { KoaSSEMiddleware } = require("@axiosleo/koapp");
root.any("/sse", async (context) => {
const func = KoaSSEMiddleware();
await func(context.koa, async () => {});
context.koa.sse.send({ data: "hello, world!" });
process.nextTick(test, context);
});
License
This project is open-sourced software licensed under MIT.
