[](https://npmjs.org/package/@axiosleo/koapp) [](https://npmjs.org/package/@axiosleo/koap
npm install @axiosleo/koapp





> A framework designed for rapid web application development with Node.js
>
> Built on Koa
``bash`
npm install @axiosleo/koapp
`bash
npx @axiosleo/koapp init
Quick Start
`javascript
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", // Use 0.0.0.0 for public access
routers: [router],
});
app.start();
// Open http://localhost:8088/test
`More Examples
- Request Validation
> See validatorjs for more rule examples
>
> See
Router examples for more usage: tests/bootstrap.js`javascript
const { Router } = require("@axiosleo/koapp");const router = new Router("/test", {
method: "any",
validator: {
// URL params, like
/test/{:id}, where 'id' is required and must be an integer
params: {
id: "required|integer",
},
query: {
name: "required|string",
},
body: {
age: "required|integer",
},
},
handlers: [],
});
`- File Operations
`javascript
// npm install @koa/multer
// npm install -D @types/koa__multerconst multer = require("@koa/multer");
root.post("/upload", async (context) => {
// Array of files
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);
});
`- Server-Sent Events (SSE)
`javascript
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);
});
``This project is open-sourced software licensed under MIT.
