cromo
v1.1.0
Published
A tiny, small & simple web framework for Bun
Downloads
108
Maintainers
Readme
🎴 Cromo
A tiny, fast & simple file-based router server for Bun 🧄
Table of Contents
Server set up
To get started with Cromo, you need to download the package using Bun:
bun add cromo
Next, import and initialize Cromo in your code:
import { Cromo } from 'cromo'
const cromo = new Cromo({
dir: './src/api' // default: './api'
port: 5000 // default: Bun.env.PORT || 3000
})
Router usage
Cromo utilizes a file-based router system. By default, the router folder is named api
, but you can change it by passing the dir option during initialization:
// File structure example
├── api
│ ├── hello
│ │ ├── [world]
│ │ │ └── index.ts
│ └── user
│ ├── [name].ts
│ └── index.ts
└── index.ts
Handlers usage
Inside the router files, you can write HTTP method handlers:
// api/hello/[world]/index.ts
import type { CromoContext, CromoHandler } from 'cromo'
// handler for GET method
export const GET: CromoHandler = (context) => {
const { world } = context.params
return Response.redirect(`https://google.com/search?q=${world}`)
}
// default handler is called if there is no specific handler for the method
export default (context: CromoContext): Response => {
return Response.json(null, 404)
}
Middlewares
There are three ways to add middlewares in Cromo:
- Using
setMiddleware
: Simply callsetMiddleware
to add an array of middlewares to the server.
// index.ts
cromo.setMiddleware([
(context, next) => {
console.log("middlewares called")
return next(context)
},
...
])
- Declaring
middlewares
const: Declare an array of middlewares in the router files asmiddlewares
const.
// api/user/index.ts
import type { CromoHandler, CromoMiddleware } from 'cromo'
export const GET: CromoHandler = ({ responseInit }) => {
return Response.json({ name: 'John' }, responseInit)
}
export const middlewares: CromoMiddleware[] = [
({ responseInit }, next) => {
responseInit.headers = {
'Access-Control-Allow-Origin': '*'
}
return next(context)
}
]
- Declaring
[METHOD]_middlewares
const: Declare an array of middlewares in the router files as[METHOD]_middlewares
const to apply middlewares to a specific method.
// api/user/[name].ts
import type { CromoHandler, CromoMiddleware } from 'cromo'
export const POST: CromoHandler = ({ params, responseInit }) => {
const { name } = params
return Response.json({ name }, responseInit)
}
export const POST_middlewares: CromoMiddleware[] = [
({ responseInit }, next) => {
responseInit.headers = {
'Access-Control-Allow-Origin': '*'
}
return next(context)
}
]
Start the server
By default, Cromo will listen to port Bun.env.PORT
, and if it is not set, it will listen to port 3000. However, you can change it by passing the port
option during initialization.
Here is an example of starting the server:
cromo.start(port => {
console.log(`Listening on port ${port}`)
})
Context object
In Cromo, we use the context object to pass data between middlewares and handlers. It contains basic Web API objects like Request
, URL
, ResponseInit
, and some useful properties like matchedRoute
, body
, params
, query
.
import { MatchedRoute } from 'bun'
export interface CromoContext {
// request objects
request: Request
url: URL
matchedRoute: MatchedRoute
body: unknown
params: Record<string, string>
query: Record<string, string>
// response objects
responseInit: ResponseInit
}