Bun入门指南

Bun+Express框架

Preview
  • Bun配置Express框架

Bun配置Express框架

我们将使用bun来安装配置Express框架。 Node.js 使用npm install命令来安装软件包,但Bun使用bun install命令来安装,还可以使用bun add命令进行安装。

  • 安装express
bun install express
//或者
bun add express

image.png

安装完成后,可以通过查看package.json文件来查看已安装的express的信息。 image.png

  • 安装@types/express

还可以安装Express的类型信息。

bun install --dev @types/express 
//或者
bun add --dev @types/express

image.png

  • 修改index.ts
import express from 'express'
const app = express()
const port = 3000
app.get('/',(req,res)=> res.send('Hello World!'))
app.listen(port,()=>console.log(`app listening on port ${port}!`))

从浏览器访问 http://localhost:3000 时,将返回“Hello World!”。 image.png

index.ts 文件使用import语句,但在Bun中你也可以使用 require,无需任何特殊设置。Bun 支持 ES 模块和 CommonJS 模块。

const express = require('express');