使用bun快速建立restful服务器
不论什么语言,快速建立web应用,需要解决3个问题:
- 客户端请求的参数服务端如何接收?
- 服务端接收到参数后,如何解析?
- 解析完请求参数后,做完业务,如何返回结果?
我将根据上面三个问题,使用bun,逐步解决:
- Setp0: 确保bun已经正确安装
$ bun -v
1.3.4
如果还未安装,请到B站看下我的安装视频:bun win11 安装 bun linux 安装 。
- Setp1: 建立bun脚手架项目
$ bun init http-app
? Select a project template - Press return to submit.
❯ Blank
React
Library
✓ Select a project template: Blank
+ .gitignore
+ CLAUDE.md
+ .cursor/rules/use-bun-instead-of-node-vite-npm-pnpm.mdc -> CLAUDE.md
+ index.ts
+ tsconfig.json (for editor autocomplete)
+ README.md
- Setp2: 改成js项目(本文使用js示例)
- 将index.ts重命名为index.js
- package.json 的module 改为 index.js
$ bun run index.js
Hello via Bun!
- Setp3: 改成js项目(本文使用js示例)
新建main.js,复制下面代码
const server = Bun.serve({
port: 3000,
routes: {
"/": () => new Response('Bun!'),
}
});
console.log(`Listening on ${server.url}`);
$ bun run main.js
Listening on http://localhost:3000/
浏览器请求 http://localhost:3000 会显示 Bun! 到这里说明你的编程环境都是好的,可以正式开始了。
- Setp4: 模拟登录
- main.js
//注意这里的导入方式
import {login} from "./login.js";
const server = Bun.serve({
port: 3000,
//设置超时时间
idleTimeout: 10,
routes: {
"/login": {
//只能POST方式
async POST(req, server) {
//设置超时时间
server.timeout(req, 60);
//拿到headers
const cookies = req.cookies;
console.log(cookies);
//拿到前端传递的body中的json数据
const loginData = await req.json();
//模拟登录
const result = await login(loginData);
//这种方式不能设置headers
return Response.json(result);
}
}
}
});
console.log(`Listening on ${server.url}`);
2.loginService.js
const { encryptPassword,verifyPassword } = require("./utils.js");
//模拟mysql中的用户表password字段
//888
const realPassword = "$2b$10$vqiXO4VvvbhuKA2vorxTN.tNk1JQIb7r/bwvqGeCF7OdjM7S9Wvty";
async function login(loginData){
// 1. 校验参数
if (!loginData.username || !loginData.password) {
return {
success: false,
message: "用户名和密码不能为空"
};
}
if(await verifyPassword(loginData.password, realPassword)){
return {
success: true,
message: "登录成功"
};
}else {
return {
success: false,
message: "密码不正确"
};
}
}
//注意这里的导出方式
exports.login = login;
- utils.js
async function encryptPassword(plainPassword){
// Bun 内置 bcrypt 加密,无需安装依赖
return await Bun.password.hash(plainPassword, {
algorithm: "bcrypt",
// 等价于 bcryptjs 的 genSalt(10)
cost: 10
});
}
async function verifyPassword(plainPassword, hashedPassword){
// 验证密码
return await Bun.password.verify(plainPassword, hashedPassword);
}
//注意这里的导出方式
exports.encryptPassword = encryptPassword;
exports.verifyPassword = verifyPassword;
$ bun --watch run main.js (--watch 修改代码后可以热部署)
Listening on http://localhost:3000/
- 客户端请求
-
body:
{
"username":"aaa",
"password":"888"
}
- headers:
key: Cookie
value: token=abc123; userId=10086; aaa=222
- 总结
- 开篇的3个问题都已经解决。
- 另外注意依赖导出和导入。
- header的获取。