Publish a convey like api employing bunjs

by:

Softwares

I arrived throughout Youtube to uncover some exciting tech movies, then I observed a online video chatting about Bun, which is a javascript runtime penned in Zig. It introduces itself as an extremely speedy runtime, way more quicker than Nodejs.

So I determined to get a try…

I acquire a search at the http server api from Bun.

In this article is how to down load bun in command line

curl -fsSL https://bun.sh/install | bash
Enter fullscreen method

Exit fullscreen method

In this article is the initially way, if the js file export a default item with fetch purpose in it, it will start out the server.

Inside indexjs

export default 
  fetch(req) 
    return new Reaction("Hi!")
  ,

Enter fullscreen mode

Exit fullscreen method

Or you can use Bun.provide

Bun.serve(
  fetch(req) 
    return new Response("Hi!")
  ,
)
Enter fullscreen mode

Exit fullscreen method

To start server, run bun run index.js

And that’s it ! All the things is working…

But this api is way too down degree, it signifies it demands builders to write duplicate code when job gets major. So I resolved to make an express like api using bun from scratch, each a single new to bun would no need to study the bun api if they have encounter on categorical.

Listed here is what I do.

Initially, we will need to manage the ask for.

We develop a BunServer course with http approach in it.

Image description

Every thing go as a result of the http approach will use delegate operate to manage.

requestMap is keep the map of path and its handler.

middlewares merchants all the middlewares person declares

Image description
Over is the delegate purpose, which we retail outlet them in cache.

And which is how we handler ask for, very uncomplicated.

Then it is the middleware.

On categorical, we have middleware perform with signature (req, res, upcoming) , it is a chain of accountability layout sample. Listed here is how I carried out.

Image description

I established a Chain course to take care of middlewares passing. Just a handful of issues you require to know here.

  1. just about every upcoming perform is called, will do index++ , we get the callback function from middlewares with that index.
  2. Prevent sign. To end the middleware from passing, we check out this.all set first, which person returns reaction from one of the middlewares. And this.isFinish() , we had already termed all the middlewares.

Eventually, we wrap items up on the fetch approach on bun api.

Image description
When the ask for will come in, we very first called all the middlewares very first, if the middleware does not quit the request, we termed the ask for handler, which we in fact how to cope with our enterprise logic.

Right here is how to use it.

The bundle is known as bunrest, it is now printed on npm

npm i bunrest
Enter fullscreen manner

Exit fullscreen method

1st develop a server item

import Application from 'bunrest'
const server = new App.BunServer()
Enter fullscreen method

Exit fullscreen mode

Then you can use it like on convey.

server.get('/test', (req, res) => 
  res.position(200).json( message: 'succeed' )
, (req, res, up coming) => 
  console.log('get')
  following()
)
server.place('/test', (req, res) => 
  res.position(200).json( concept: 'succeed' )
, (req, res, upcoming) => 
  console.log('put')
)
server.publish('/test', (req, res) => 
  res.position(200).json( information: 'succeed' )
)
Enter fullscreen manner

Exit fullscreen manner

Include middlewares

// or you can include the middlewares this way
server.get('/user', 
    (req, res) =>  /** deal with ask for **/ , 
    (req, res, future) => 
      /**
       *  Manage middlewares
       * */
    )
Enter fullscreen method

Exit fullscreen method

Include router

// include router
const router = server.Router()

router.get('/test', (req, res) => 
  res.position(200).json( information: 'Router succeed' )
)

router.write-up('/test', (req, res) => 
  res.status(200).json( message: 'Router succeed' )
)

router.set('/test', (req, res) => 
  res.position(200).json( concept: 'Router succeed' )
)

server.use('/your_route_path', router)
Enter fullscreen method

Exit fullscreen method

Last but not least, to commence the server

server.pay attention(3000, () => 
  console.log('App is listening on port 3000')
)
Enter fullscreen mode

Exit fullscreen method

If you want to checkout the supply code or contribute, here is the github webpage

Leave a Reply

Your email address will not be published.