Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/datkat21/bun-silly-web-server

simple and minimal server library for bun.js?
https://github.com/datkat21/bun-silly-web-server

Last synced: 7 days ago
JSON representation

simple and minimal server library for bun.js?

Awesome Lists containing this project

README

        

# silly-web-server

Silly web server for Bun.js. :P
This little thing extends `Bun.serve()` and creates a simple wrapper with useful features such as a public directory, websocket support built-in, etc.

## Usage

This isn't exactly a library (yet) so you'll have to copy the files from `handlers` into your project.

Here are some server examples:

**Basic HTTP server**
```ts
import { requestHandler } from "./handlers/requestHandler";
import { ReqHandlerType, makeServer } from "./handlers/serverConstructor";

const server = makeServer({
// Set up the server basics
port: 3000,
publicFolderPath: "./public",

// Set up the request handler
requestHandler,
requestHandlerType: ReqHandlerType.UrlOnly
});

console.log(`Listening on localhost:${server.port}`);
```

**Basic websocket server**
```ts
import { requestHandler } from "./handlers/requestHandler";
import { ReqHandlerType, makeServer } from "./handlers/serverConstructor";

const server = makeServer({
// Set up the server basics
port: 3000,
publicFolderPath: "./public",

// Set up the request handler
requestHandler,
requestHandlerType: ReqHandlerType.UrlAndWs,

// Instance of WebSocketHandler
websocketConfig: {
message(ws, message) {
console.log(`${ws.data} said: ${message}`);
},
// . . . (open, close, ping, pong, etc.)
},
// Also see websocketHandler to set custom `ws.data`.
});

console.log(`Listening on localhost:${server.port}`);
```

## Setup

To install dependencies:

```bash
bun install
```

To run:

```bash
bun run index.ts
```

This project was created using `bun init` in bun v1.0.0. [Bun](https://bun.sh) is a fast all-in-one JavaScript runtime.