Skip to content

ebrardev/bun-tutorial

Repository files navigation

Bun Tutorial

Installation

npm -g bun

Setup

bun init

Start

bun run index.ts

Create server

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

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

Watch mode

bun --watch index.ts

Hot Mode

bun --hot index.ts

Enviroment variables

no need install dotenv packages

Scripts

  "scripts": {
    "dev": "bun --watch  index.ts",
    "start": "bun run index.ts"

  },

Simple route

      const url= new URL(req.url);
      if(url.pathname === "/")   return new Response("Home page");
  },

Node modules

   import path from 'path';


const filepath = path.join("src", "modules", "index.ts");
const filename = path.basename(filepath);
console.log(filename)

File I/O

   const data = "lorem ipsum dolor sit amet"

await Bun.write("index.html", data)

const file = await Bun.file("index.html")
console.log(await file.text())
console.log(file.size)
console.log(await file.slice(0, 5))
console.log(await file.stream())
console.log(await file.arrayBuffer())

Bun test

import {describe,expect,test,beforeAll} from "bun:test";

Bundler

bun build ./src/index.ts --outfile=./dist/bundle.js

Bundler watch mode

bun build ./src/index.ts --outfile=./dist/bundle.js --watch

react & jsx

app.tsx
bun install react  react-dom 

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published