Skip to content

Bun 速查表

快速的一体化 JavaScript 运行时、打包器与包管理器。

01

快速上手

Hello World

Bun 基于 Zig 构建的运行时可原生运行 JS/TS。

bun
console.log("Hello, World!");

运行

Bun 可以直接替代 node 使用。

bun
# bun run main.ts
# bun main.ts

服务器

使用 Bun.serve 创建 HTTP 服务器。

bun
export default {
  port: 3000,
  fetch(req) {
    return new Response("Hello!");
  }
};
02

API

文件读写(File I/O)

使用 Bun.file 快速读取文件。

bun
const text = await Bun.file("file.txt").text();
const buf = await Bun.file("data.bin").arrayBuffer();

SQLite

内置 SQLite 支持。

bun
import { Database } from "bun:sqlite";
const db = new Database("mydb.sqlite");
const query = db.query("SELECT * FROM users");
console.log(query.all());

哈希

内置密码哈希功能。

bun
const hash = await Bun.password.hash("secret");
const ok = await Bun.password.verify("secret", hash);

这篇内容对您有帮助吗?