Skip to content

Instantly share code, notes, and snippets.

@SaicharanKandukuri
Created February 8, 2023 09:05
Show Gist options
  • Save SaicharanKandukuri/9d50e8bdd6a6229e9ab4fbc8f2db18b4 to your computer and use it in GitHub Desktop.
Save SaicharanKandukuri/9d50e8bdd6a6229e9ab4fbc8f2db18b4 to your computer and use it in GitHub Desktop.

πŸš€ Bun with ExpressJS

download

A quick server with bun and expressJS with typescript

πŸ“¦ Install

mkdir express-with-bun
cd express-with-bun
bun init

and go through the wizard to create a new project ( just hit enter to accept the defaults )

πŸ“¦ Install dependencies

bun install
bun install "@types/express" "express"

Note: bun will install the dependencies in the package.json file just like npm or yarn

This sets up all the requirements

πŸ“ Code

in index.ts add the following code

import express, { Express, Request, Response } from 'express';

const app: Express = express();
const port = 30001;

app.get('/', (req: Request, res: Response) => {
    res.send('Hello world!');
});

app.listen(port, () => {
    console.log(`⚑️[server]: Server is running at http://localhost:${port}`);
});

EXPLANATION: This is a simple express server that listens on port 30001 and returns Hello world! when you hit the root url /

πŸš€ Run

bun run index.ts

πŸ“ Output

⚑️[server]: Server is running at http://localhost:30001

navigate to http://localhost:30001 and you should see Hello world!

use curl http://localhost:30001 to see the output in the terminal

here is a screenshot of the output

image image

import express, { Express, Request, Response } from 'express';
const app: Express = express();
const port = 30001;
app.get('/', (req: Request, res: Response) => {
res.send('Hello world!');
});
app.listen(port, () => {
console.log(`⚑️[server]: Server is running at http://localhost:${port}`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment