๐ 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)