Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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?
- Host: GitHub
- URL: https://github.com/datkat21/bun-silly-web-server
- Owner: datkat21
- Created: 2023-09-09T02:15:18.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-09-09T02:27:08.000Z (over 1 year ago)
- Last Synced: 2024-04-28T06:06:51.603Z (8 months ago)
- Language: TypeScript
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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.