I’m pretty excited about the launch of Bun.

If you don’t know, Bun is a toolkit for JavaScript and TypeScript apps. It is basically a JavaScript runtime similar to Node.js.

However, it claims to be many times faster than Node.js and also acts as a drop-in replacement for Node.js.

It’s written in Zig and uses Apple’s JavaScriptCore engine instead of the V8 engine used by Node.js and other Chromium browsers.

In this post, I’m going to introduce Bun and also explain how you can create a simple HTTP Server with Bun.

1 – Features of Bun

The goal of Bun is to help the JavaScript community become more productive.

Some of the features provided by Bun in this regard are as follows:

  • Bun processes start 4X faster than Node.js currently.
  • Bun provides out-of-the-box support for TypeScript and JSX. You can basically execute .jsx, .ts and .tsx files without any additional setup. Bun’s transpiler converts these to vanilla JavaScript before execution.
  • It provides support for both ES Modules (ESM) as well as CommonJS.
  • Bun also implements standard Web APIs like fetch and WebSocket.
  • Lastly, Bun aims for full compatibility with built-in Node.js globals such as process and core modules such as path, fs, http and so on.

As per the makers of Bun, their long-term goal is to be a key infrastructural toolkit for building apps with JS/TS and include a package manager, transpiler, bundler, test runner and so on.

2 – Getting Started with Bun

Before we can create an HTTP Server with Bun, we need to install it on our machine.

Bun ships as a single executable that can be installed based on your operating system.

You can also install Bun using the Node Package Manager (NPM).

$ npm install -g bun

On Mac & Linux systems, the best way is to use the installation script with the below command.

$ curl -fsSL https://bun.sh/install | bash 

In the next step, you can create a project directory for your first Bun project.

$ mkdir my-first-bun-experience
$ cd my-first-bun-experience

Then, run the bun init command to scaffold a new project.

$ bun init

This will open an interactive command line tool. You can simply use the default answers and press enter to continue.

Bun will automatically generate a starter package. Since the default entry point is a *.ts file, Bun generates a tsconfig.json file as well.

If you open the index.ts file, you’ll find a simple console.log() statement as follows:

console.log("Hello via Bun!");

To run the file from the shell, just execute the below command.

$ bun index.ts

You’ll see the “Hello via Bun!” message printed to the console.

3 – HTTP Server with Bun

Now that you have a working project with Bun, time to create a HTTP server.

Open the index.ts file and replace the existing code with the below code.

const server = Bun.serve({
  port: 3000,
  fetch(req) {
    return new Response("Hello World!");
  },
});

console.log(`Listening on http://localhost:${server.port} ...`);

Basically, the Bun.serve() API is used to start an HTTP server in Bun. It takes the port and fetch function as input.

The fetch handler handles the incoming requests. It receives a Request object and returns a Response object.

The serve() method returns a Server object.

Just this much is also enough to build a working HTTP Server in Bun.

You can now start the HTTP server using the below command:

$ bun index.ts

However, a better approach is to add a dedicated script to the scripts section of the package.json file.

See below:

"scripts": {
  "start": "bun run index.ts"
},

With this script in place, we can run bun run start to start the HTTP server on localhost:3000.

Conclusion

Starting off with an HTTP server in Bun is quite simple.

In this post, we looked at the step-by-step approach to install Bun on your system and create a web server using the Bun.serve() API.

If you have any comments or queries, please mention them in the comments section below.

Categories: Bun

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *