![require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages](https://cdn.sanity.io/images/cgdhsj6q/production/be8ab80c8efa5907bc341c6fefe9aa20d239d890-1600x1097.png?w=400&fit=max&auto=format)
Security News
require(esm) Backported to Node.js 20, Paving the Way for ESM-Only Packages
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Start your Koa project with necessary Boring codes.
npm i --save kobp
# OR
yarn kobp
NOTE we listed koa
as our peerDependencies
so please include the koa
in your own codebase.
Start your node.js TypeScript project and describe your endpoints with controller style.
To expose each method as routes. Use our built-in decorator. Route
which accepts method, paths, and Koa's middlwares.
controllers/hello.cotnroller.ts
import type { KobpServiceContext } from 'kobp'
import { Route, BaseRoutedController } from 'kobp'
export class HelloController extends BaseRoutedController {
@Route('post', '/echo')
async migrate(context: KobpServiceContext) {
return context.request.body
}
@Route()
async index(context: KobpServiceContext) {
return {
hello: 'world'
}
}
}
Or you can describe your controllers in a classical way. (Avoid using decorators). This method introduce less code when it is bundled.
controllers/hello.controller.ts
import type { KobpServiceContext } from 'kobp'
import { RouteMap, BaseRoutedController } from 'kobp'
export class HelloController extends BasedRouteController {
public getRouteMaps(): RouteMap {
return {
...super.getRouteMaps(),
index: { method: 'get', path: '/', middlewares: [] }, // Same as our decorator above.
}
}
async index(context: KobpServiceContext) {
return {
hello: 'world'
}
}
}
Now to utilise this controller. Here is how we start a module.
// routes.ts
import { KobpServiceContext, KobpServiceState } from 'kobp'
import Router from 'koa-router'
import { HelloController } from 'src/controller/HelloController'
export const makeRoutes = (): Router => {
const api = new Router<KobpServiceState, KobpServiceContext>()
api.use('/hello', ...new HelloController().getMiddlewares() as any)
return api
}
And also ...
// server.ts
import {
BootstrapLoader,
BootstrapModule,
} from 'kobp'
import { makeRoutes } from "./routes"
// Finally
const run = async () => {
const loader = new BootstrapLoader()
const app = await loader
.addModule(new BootstrapModule(['json'])) // type of input body it should support.
.build(makeRoutes(), {}) // returns Koa App
app.listen(9000, '0.0.0.0')
}
run()
By the example above. You will be able to:
curl http://localhost:9000/hello/
# OR
curl -XPOST http://localhost:9000/hello/echo -H 'content-type: application/json' -d '{"some":"key","json":"value"}'
See other Example for more info.
import {
BootstrapModule,
KobpRouter,
} from 'kobp'
import { makeLambdaHandler } from 'kobp-lambda'
import { HelloController } from '@controllers/hello.controller'
const router = new KobpRouter()
new HelloController().register('/hello', router)
export default makeLambdaHandler(router, {
customizer: (loader) => {
loader.addModule(new BootstrapModule(['json']))
},
binary: true, // Enable return as binary!
})
Note that most of the time AWS's Lambda doesn't support return the Response with Binary content. To make it so please make sure you enabled binary
mode as per example above.
Sometime we need to understand what's going on under the hood of our custom made feature such as RequestContext. Attach the message logging by declare
ENV: KOBP_DEBUG
to Yes
or True
or 1
to let the framework emit debugging messages.
[/] Example repo
[/] Modularized
[/] Core module
[/] Mikroorm module
[/] Publish with microbundle instead.
[/] Lambda Handler
[ ] SNS/SQS Handler
FAQs
Koa Boilerplate with MikroORM
The npm package kobp receives a total of 295 weekly downloads. As such, kobp popularity was classified as not popular.
We found that kobp demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 0 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
require(esm) backported to Node.js 20, easing the transition to ESM-only packages and reducing complexity for developers as Node 18 nears end-of-life.
Security News
PyPI now supports iOS and Android wheels, making it easier for Python developers to distribute mobile packages.
Security News
Create React App is officially deprecated due to React 19 issues and lack of maintenance—developers should switch to Vite or other modern alternatives.