Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can Bunjs be used as a backend server?

Tags:

bun

Now we can start a react App with bun as a server

Can we use Bunjs as complete backend server?

For Example, Can bun run this code?

const express = require('express')
const app = express()

app.get('/', (req, res) => {
  res.send('hello world')
})

app.listen(3000)
like image 251
Mohamed Ibrahim Avatar asked Oct 26 '25 12:10

Mohamed Ibrahim


2 Answers

I guess Bun does not YET implement all node.js api's. I tried http and it seems currently missing. And as much I understand it currently has its own built-in HTTP server.

Check the "Getting started" section on -> https://bun.sh/

A sample server:

    export default {
      port: 3000,
      fetch(request) {
        return new Response("Welcome to Bun!");
      },
    };

(This example reminds me of serverless functions.) As this is the case, it seems you can not rely on Node.js http, or most probably any server framework like express.

At least for now, bun's roadmap (https://github.com/oven-sh/bun/issues/159) shows a line, which I am not sure is talking about node's http server or sth. else about Bun's own server.

Once complete, the next step is integration with the HTTP server and other Bun APIs

like image 61
Alper Batıoğlu Avatar answered Oct 29 '25 07:10

Alper Batıoğlu


Yes you can, and you don't even need an dependencies (like Express). Your example can be written as simply as:

Bun.serve({
  port: 3000,
  fetch() {
    return new Response('hello world');
  },
});

console.log(`Running on port: ${serve.port}`)
bun run index.js 

Then open http://127.0.0.1:3000 in a browser.

See the Bun HTTP Docs for more server options like out-of-the box TLS support and WebSockets. Considering the benchmarks of Bun it's very capable as a backend server. Also see Elysia as a very complete Bun based backend framework that [on their homepage] claims to handle 17 x more request/second that Express.

like image 23
MountainAsh Avatar answered Oct 29 '25 09:10

MountainAsh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!