The JS runtimes
Published in

The JS runtimes

How do I write a hello world server in Bun?

Have you just started exploring Bun and wonder how to go about writing a very simple hello world server? If yes, this quick article will show you the three common ways to write a basic hello world server. Let’s get started.

Way 1 — Native server using default export

Bun, like others, comes with a native HTTP server. The first and easiest way to bring up the native server is by making a default export from the main module that, at minimum, contains:

  • fetch: A fetch function that takes a web standard Request as input and returns a web standard Response
  • port: This is optional (default is 3000)
export default {
fetch(request) {
return new Response("Hello world");
},
};

The above code will start a native HTTP server listening on port 3000. Let’s test it out:

> curl http://localhost:3000 
Hello world

Now, let’s change the port to 8080:

export default {
port: 8080,
fetch(request) {
return new Response("Hello world");
},
};

A quick test:

> curl http://localhost:8080
Hello world

Way 2 — Native server using Bun.serve API

If you don’t want to use the default export, which will likely be the case with production apps that runs into tens or hundreds of files, the native Bun.serve API can be used to start a server from any place. The inputs are exactly the same as you’ve provided in the way 1. Here is the code for the same hello world server using Bun.serve:

Bun.serve({
port: 8080,
fetch(request) {
return new Response("Hello world");
},
});

As we see that the inputs are exactly the same. Now, let’s run a quick test using curl:

> curl http://localhost:8080
Hello world

Way 3 — Using express

If you don’t like to use the native server because it’s way too basic, then the third way is to use any NPM module like express. I chose express because it is still extremely popular (~25M weekly downloads).

The following hello world code has been taken directly from express’s home page:

const express = require('express')
const app = express()
app.get('/', function (req, res) {
res.send('Hello World')
})
app.listen(3000)

I can run this code directly in bun:

> bun run helloWorld.js

The server starts quickly, as if it’s running in Node.js. Here is a quick test using curl (I’m showing response headers, so we can see that the response in indeed coming from the express framework):

> curl http://localhost:3000
< HTTP/1.1 200 HM
< Content-Type: text/html; charset=utf-8
< ETag: W/"b-Ck1VqNd45QIvq3AZd8XYQLvEhtA"
< X-Powered-By: Express
< Content-Length: 11
<
Hello World

The express app works perfectly in Bun!

That was about the three ways of writing a very simple hello world server in Bun. Thanks for reading!

--

--

Articles on the popular JS runtimes, Node.js, Deno, and Bun

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store