ionited / mesh

A fast web framework based on uWebSocket.js
MIT License
30 stars 3 forks source link
api backend express fiber http mesh rest socket uwebsocketsjs web

Mesh

[!WARNING] This is Mesh. A fast web framework based on uWebSocket.js

[!NOTE] The project was renamed from Fiber to Mesh to avoid confusion with other Web Framework project

About uWebSockets.js

µWebSockets.js is a web server bypass for Node.js that reimplements eventing, networking, encryption, web protocols, routing and pub/sub in highly optimized C++. As such, µWebSockets.js delivers web serving for Node.js, 8.5x that of Fastify and at least 10x that of Socket.IO. It is also the built-in web server of Bun.

Quick start

Install with NPM

npm i @ionited/mesh

Documentation

App

import { App } from '@ionited/mesh';

const app = new App();

app

.use((req, res) => console.log('Hello World!'))

.catch((e, req, res) => res.status(e.status ?? 500).json({ message: e.message ?? 'Internal server error' }))

.notFound((req, res) => res.status(404).end())

.any('/users', (req, res) => res.json({ success: true }))

.del('/users', (req, res) => res.json({ success: true }))

.get('/users', (req, res) => res.json({ success: true }))

.options('/users', (req, res) => res.json({ success: true }))

.post('/users', (req, res) => res.json({ success: true }))

.put('/users', (req, res) => res.json({ success: true }))

.ws('/ws', {
  close: (ws, code, message) => {},
  message: (ws, message) => {},
  open: ws => {}
})

.listen(1000);

Router

import { Router } from '@ionited/mesh';

const router = new Router('/public');

router

.use((req, res) => console.log('Hello World!'))

.any('/users', (req, res) => res.json({ success: true }))

.del('/users', (req, res) => res.json({ success: true }))

.get('/users', (req, res) => res.json({ success: true }))

.options('/users', (req, res) => res.json({ success: true }))

.post('/users', (req, res) => res.json({ success: true }))

.put('/users', (req, res) => res.json({ success: true }))

.ws('/ws', {
  close: (ws, code, message) => {},
  message: (ws, message) => {},
  open: ws => {}
});

const routes = router.routes();

app.routes(routes);

HttpRequest

interface HttpRequest {
  body(): Promise<{ [key: string]: any }>;
  data: { [key: string]: any };
  files(): Promise<{ [key: string]: UploadedFile | undefined }>;
  headers(): { [key: string]: string };
  method(): string;
  params(): { [key: string]: string };
  query(): { [key: string]: any };
  route: string;
  url(): string;
}

HttpResponse

interface HttpResponse {
  end(text?: string): void;
  header(key: string, value: string): this;
  json(json: any): void;
  send(text: string): void;
  sendFile(path: string): Promise<void>;
  status(status: number): this;
}

UploadedFile

interface UploadedFile {
  data: ArrayBuffer;
  filename: string;
  type: string;
}

WebSocket

interface WebSocket {
  send(message: string): void;
}

WebSocketBehavior

export interface WebSocketBehavior {
  close?: (ws: WebSocket, code: number, message: ArrayBuffer) => void;
  message?: (ws: WebSocket, message: ArrayBuffer) => void;
  open?: (ws: WebSocket) => void;
}

License

Copyright (c) 2023 Ion. Licensed under MIT License.

https://ionited.io