GithubHelp home page GithubHelp logo

hyperbun's Introduction

HyperBun is meant for Bun runtimes, it will not work in Node / Deno.

A simple HTTP routing library built on top of Bun's built in HTTP solution.

Getting started

bun add hyperbun

Example

import {createServer} from 'hyperbun';

const server = createServer();

server.middleware((request, context) => {
  console.log('Just a simple middleware...');
});

server.middleware((request, context) => {
  return Error('Oops, I returned a 500.');
});

server.get('/json', (request, context) => {
  return {
    hello: 'I will automatically become a JSON response...'
  }
});

server.get('/text', (request, context) => {
  return "Hello, I will be a text/html response...";
});

server.listen({
  port: 3000,
  hostname: '0.0.0.0'
});

Request Context

Includes {params: {}, query: {}} by default. More to come.

import {createServer} from 'hyperbun'

const server = createServer();

server.middleware((request, context) => {
  context.auth = {user: '1234'};
});

// /home?search=movies
server.get('/home', (_, context) => {
  console.log(context.query) // { search: "movies" }
  return 'OK';
})

server.get('/private', (_, context) => {
  if (context.auth?.user !== '1234') {
    // Return your own custom responses too.
    return new Response('unauthorized', {
      status: 401,
    })
  }

  return {
    private: 'data',
  }
})

server.listen({port: 3000});

Dynamic routes with params

import {createServer} from 'hyperbun';
const server = createServer();

server.post('/users/:userId', async (request, context) => {
  const {userId} = context.params;
  const updatePayload = await request.json();

  await UserModel.updateById(userId, updatePayload);
  return { success: "true" };
});

server.listen({
  port: 3000,
  hostname: "0.0.0.0",
});

Send a file response

import {createServer, asAttachment} from 'hyperbun';

const server = createServer();

server.get('/file', () => {
  return Bun.file('./test-file.txt'); // Inline
})

server.get('/file', () => {
  return asAttachment('./test-file.txt', { // Attachment (download)
    name: 'helloworld.txt'
  });
})

server.listen({
  port: 3000,
  hostname: '0.0.0.0',
});

Consuming a request body

import {createServer} from 'hyperbun'

const server = createServer();

server.post('/users/add', async (request, context) => {
  const user = await request.json();
  const result = await database.create(user);
  return result;
});

Create multiple Routers and use on base path

import {createServer, createRouter} from 'hyperbun';

const server = createServer();
const router1 = createRouter();
const router2 = createRouter();

server.get('/', () => new Response('/ server main router'));

router1.get('/', () => new Response('/users router'));
router2.get('/', () => new Response('/posts router'));

server.use('/users', router1);
server.use('/posts', router2);

server.listen({
  port: 3000,
})

Available methods

These listeners will automatically match the route and method that you setup and respond with a 404 for ones you don't have.

server.get server.post server.put server.delete server.patch

hyperbun's People

Contributors

eckhardt-d avatar

Stargazers

Tomas Čerkasas avatar Gregor Gilchrist avatar Utsav Ojha avatar Roberto Canha avatar Henry Goodwin avatar  avatar Axl Cuyugan avatar  avatar Kunal Dabir avatar Thiago Francisco Dias avatar Marcis Bergmanis avatar Nicholas Biantoro avatar  avatar Yash Gupta avatar Dax avatar Yusuke Wada avatar BNS avatar Uli Troyo avatar

Watchers

James Cloos avatar Nicholas Biantoro avatar  avatar

Forkers

leonardoraele

hyperbun's Issues

request return 404

Hi,! I am interested with this project.

I just build a fresh repo and run bun add hyperbun
and copy your example, after I copy your example and run bun run [filename].
It is not working, the terminal print

[3.00ms] bun!! v0.5.0 (2db04ef9)


  Link: http://localhost:3000


404 GET /text as application/octet-stream
404 GET /text as application/octet-stream

after I request from Google Chrome. I don't have any idea what happen.

OS: MacOS 13

Project is missing a license

I'm interested in the project, but it lacks a license right now, so I can't really use it in a real project yet.

The package.json has a "license": ""MIT" field, but I'm not sure it means anything without the actual text of the license somewhere in the repository.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.