π Getting Started with Oxarion: Practical Usage Guide
Are you looking for a fast, modern, and easy-to-use Bun.js web framework? Oxarion is here to help you build web servers, APIs, and real-time apps with minimal fuss. In this post, Iβll show you how to get up and running with Oxarion, with practical code examples you can use right away.
β οΈ Note: Oxarion is built specifically for Bun. You must use Bun.js to run Oxarion projectsβNode.js is not supported!
β‘ Installation
First, install Oxarion in your project:
bun add oxarionjs
π Creating Your First Server
Letβs create a simple HTTP server that responds with "Hello, Oxarion!".
// myServer.js
import Oxarion from "oxarionjs";
Oxarion.start({
port: 3000,
httpHandler: (router) => {
router.addHandler("GET", "/", (_, res) => {
res.send("Welcome to Oxarion");
});
},
});
Start your server and visit http://localhost:3000 in your browser!
π§© Using Middleware
Middleware lets you add custom logic to your request/response pipeline. Hereβs how to log every request:
router.middleware("/", (req, res, next) => {
console.log(`${req.method} ${req.url}`);
next();
});
You can add as many middleware functions as you like, for things like authentication, parsing, or error handling.
π£οΈ Defining Routes
Oxarion makes it easy to define routes for different HTTP methods and paths:
router.addHandler("GET", "/get-req", (req, res) => {
res.send("This is GET Request");
});
router.addHandler("POST", "/post-req", (req, res) => {
res.send("This is POST Request");
});
You can access query parameters, request bodies, and more via the req object.
π Real-Time with WebSockets
Building a chat app or real-time dashboard? Oxarion has built-in WebSocket support:
// myWs.js
import Oxarion from "oxarionjs";
Oxarion.start({
port: 3000,
httpHandler: (router) => {
// Upgrade path to accept WebSocket
router.switchToWs("/ws");
},
wsHandler: (watcher) => {
// Path watcher to handle incoming data
watcher.path("/ws", {
onOpen: (ws) => {
console.log("Ws Opened");
},
onClose: (ws, code, reason) => {
console.log(`Ws Closed: Code: ${code}, Reason: ${reason} `);
},
onDrain: (ws) => {
console.log("Ws Drained");
},
onMessage: (ws, message) => {
console.log(`Incoming Message: ${message.toString()}`);
},
});
},
});
Connect to ws://localhost:3000/ws with your favorite WebSocket client!
π§ͺ Try the Examples
Check out the examples/ folder in the repo for more sample code:
-
simple_server.jsβ Basic HTTP server -
middleware.jsβ Using middleware -
routes_wrapper.jsβ Advanced routing -
websocket_server.jsβ WebSocket server
π Wrapping Up
Oxarion is designed to get you building web apps and APIs quickly, with a clean and modern API. Whether youβre prototyping or building production apps, give Oxarion a try!
Questions or feedback? Drop a comment below or open an issue on GitHub!
Top comments (0)