Awesome
Microraptor
Microraptor is a lightweight framework for easy routing on top of deno network lib. Project is a work-in-progress and in pre-alpha.
Install
Import Microraptor in your project with just one line of code:
import { Microraptor } from "https://deno.land/x/microraptor/microraptor.ts";
How to use (basic)
A basic example of usage is creating a new file named for example web.ts
:
import { Microraptor, Method, MicroRequest } from "../microraptor.ts";
const server = new Microraptor({ port: 3000 });
server.route({
method: Method.get,
path: "/",
controller: {
response: (req: MicroRequest) => {
req.request.respond({ body: "Hello Microraptor!" });
},
},
});
server.start();
Then in your terminal you can run:
$ deno run --allow-net web.ts
You can discover a bit more for now in /examples
directory.
Parameters
Parameters are easily accessible from .param
variable of MicroRequest
.
server.route(
{
method: Method.get,
path: "/:name",
controller: {
response: (req: MicroRequest) => {
req.request.respond({ body: `Hello ${req.param.name}` });
},
},
},
);
Querystring
Querystring are easily accessible from .query
variable of MicroRequest
.
server.route(
{
method: Method.get,
path: "/",
controller: {
response: (req: MicroRequest) => {
req.request.respond({ body: `Hello ${req.query.name}` });
},
},
},
);
Body
Body is easily accessible from .body
variable of MicroRequest
.
server.route(
{
method: Method.post,
path: "/",
controller: {
response: (req: MicroRequest) => {
req.request.respond({ body: `Hello ${req.body.name}` });
},
},
},
);
Cookie
Cookies are easily accessible from .cookie
variable of MicroRequest
.
server.route(
{
method: Method.get,
path: "/",
controller: {
response: (req: MicroRequest) => {
req.request.respond({ body: `Hello ${req.cookie.name}` });
},
},
},
);
Validation
Validation in Microraptor is powered by fossil/ a library that easily validate values and types.
A simple usage can be the following:
server.route(
{
method: Method.get,
path: "/:country/:city",
controller: {
response: (req: MicroRequest) => {
req.request.respond(
{
body: JSON.stringify(
{
param: req.param,
query: req.query,
cookie: req.cookie,
body: req.body,
},
),
},
);
},
},
validation: new Validation(
{
param: [
new MicroValidator(
"country",
ValidatorType.string,
["Italy", "italy"],
),
],
},
),
},
);
Pending implementation
- Querystring
- Params
- Cookie
- CORS
- Middleware
- Response type (text, json)
- AWS Lambda support
- Test
Credits
Icon made by Freepik from www.flaticon.com