β A comprehensive framework for server-side applications. Being built on top of TypeScript, it surprisingly supports native Web APIs. It can also be used for REST/GraphQL and many others.
Repository
Current version released
3 years ago
Versions
Ion Framework
β A comprehensive framework for server-side applications. Being built on top of TypeScript, it surprisingly supports native Web APIs. It can also be used for REST/GraphQL and many others.
Quick start
// app.ts
import { Server } from "https://deno.land/x/ion@0.1.3/mod.ts";
const server = new Server();
server.on("GET /", () => "Welcome!");
await server.listen();
Run server
deno run --allow-net app.ts
IonContext
server.on("POST /user", ({ req, res }: any) => {
const accessToken = req.headers["Access-Token"];
// your access token logic...
if (accessToken === "X") {
res.status = 201;
return { id: 1, name: "Deno" };
} else {
res.status = 401;
}
});
IonRequest
// www.example.com/books/1?language=en
server.on("GET /books/:bookId", ({ req }: any) => {
console.log(req.method); // GET
console.log(req.url); // URL Object
console.log(req.headers); // Request Headers
console.log(req.query); // { language: "en" }
console.log(req.params); // { bookId: "1" }
console.log(req.body); // { name: "Deno" } (POST body: application/json only)
});
IonResponse
server.on("GET /example", ({ res }: any) => {
res.headers["Access-Control-Allow-Origin"] = "*";
});
Custom Events
server.on("foo", (x: number, y: number, z: number) => console.log(x + y + z)); // 6
server.on("GET /bar", () => {
server.dispatch("foo", 1, 2, 3);
});
Custom Options
const server = new Server({ host: "127.0.0.0", port: 3000 }); // default 0.0.0.0:8080
Allowed Methods
server.on("GET /", () => null);
server.on("PUT /", () => null);
server.on("POST /", () => null);
server.on("PATCH /", () => null);
server.on("DELETE /", () => null);
server.on("OPTIONS /", () => null);
Built-in events
// on server listen
server.on("LISTEN", ({ host, port }: any) => console.log(host, port));
// on request (a.k.a middleware)
server.on(
"REQ",
({ req, res, onComplete }: any) => console.log(req, res, onComplete),
);
Middleware Support
// cors middleware
server.use(({ res }: any) => {
res.headers["Access-Control-Allow-Origin"] = "*"
})
// alternative way (preferred)
server.on("REQ", ({ res }: any) => {
res.headers["Access-Control-Allow-Origin"] = "*"
})
server.on("REQ", ({ onComplete }) => {
const start - performance.now()
onComplete(() => {
const end = performance.now()
console.log(`RequestTime: ${end - start}ms`)
})
})
IonRefs (a.k.a global props)
server.refs.x = 42
server.on("GET /", ({ req, res}: any, refs: any) => {
console.log(refs.x) // 42
});