|
6 | 6 |
|
7 | 7 | ---
|
8 | 8 |
|
9 |
| -# <p align="center">Buxt</p> |
10 |
| - |
11 |
| -## <p align="center">Filesystem-route based REST API server for Bun.js</p> |
12 |
| - |
13 |
| -<p align="center">If you've ever used NextJs's routing system, then you should already be familiar with this concept. In short, it creates routes based off the directory structure under the folder "routes" in the root of the project. Exporting a defult function is all you need to do to get things working, the rest is handled automatically. |
| 9 | +<p align="center"> |
| 10 | +<img src="./.gitfiles/buxt-logo.png"> |
| 11 | + |
| 12 | +<h3 align="center"><b>Lightweight filesystem-based router for creating REST APIs.</b> |
14 | 13 |
|
15 | 14 | ---
|
16 | 15 |
|
17 | 16 | ## <p align="center"> **Note**
|
18 | 17 |
|
19 |
| -<p align="center"> This project is currently not usable - if you try to use it, it will just return 404. |
| 18 | +<p align="center"> This project is currently very early development, and I wouldn't recommend it for production. Feel free to use it as you like and if you find any problems then submit an issue via github issues tab. |
20 | 19 |
|
21 | 20 | ---
|
22 | 21 |
|
23 | 22 | ## <p align="center"> Features/Roadmap
|
24 | 23 |
|
25 |
| -- [x] MVP Status |
26 | 24 | - [x] Nested routing
|
27 | 25 | - [x] Route parameters
|
28 | 26 | - [ ] Catch-all routes on same level as named routes
|
29 | 27 | - [x] Implement response handling logic fully
|
30 | 28 | - [ ] Middleware
|
31 | 29 | - [x] CORS solution
|
32 |
| -- [ ] More detailed usage instructions |
| 30 | +- [ ] Advanced Logging |
| 31 | +- [ ] Authentication |
| 32 | +- [ ] Websockets |
| 33 | +- [ ] ESLint plugin |
| 34 | +--- |
| 35 | + |
| 36 | +## <b>Installation</b> |
| 37 | +`bun a buxt` |
| 38 | + |
| 39 | +## <b>Getting started</b> |
| 40 | +Starting a basic server with the default values |
| 41 | + |
| 42 | +``` typescript |
| 43 | +//index.ts |
| 44 | +import CreateServer from "buxt"; |
| 45 | + |
| 46 | +await CreateServer(3000).then(s => s.listen()); |
| 47 | + |
| 48 | +//routes/example_endpoint.ts |
| 49 | +import type { BuxtRequest, BuxtResponse } from "buxt"; //typings arent required, but useful! |
| 50 | + |
| 51 | +export default async function(req: BuxtRequest, res: BuxtResponse) { |
| 52 | + res.send("Hello!"); |
| 53 | +} |
| 54 | +``` |
| 55 | +Thats it! |
| 56 | + |
| 57 | +--- |
| 58 | + |
| 59 | +## <b>Usage</b> |
| 60 | + |
| 61 | +By default, the app will search for exported functions under <b>\<project-root>/routes</b> and <b>\<project-root>/src/routes</b>, unless specified when creating the server. |
| 62 | + |
| 63 | +Aside from the previous example, there are three other ways of creating and starting a buxt server: |
| 64 | + |
| 65 | + |
| 66 | +### <b><u>Create a server with default route root using port 3000</u></b> |
| 67 | +``` typescript |
| 68 | +//index.ts |
| 69 | +import CreateServer from "buxt"; |
| 70 | + |
| 71 | +const server = await CreateServer(3000); |
| 72 | +await server.listen(); |
| 73 | +``` |
| 74 | + |
| 75 | +### <u><b>Create a server with default route root using port 3000 and a custom root route path</b></u> |
| 76 | + |
| 77 | +``` typescript |
| 78 | +//index.ts |
| 79 | +import CreateServer from "buxt"; |
| 80 | + |
| 81 | +const server = await CreateServer(3000, "src/api"); |
| 82 | +await server.listen(); |
| 83 | +``` |
| 84 | + |
| 85 | +### <u><b>Create a server with config object</u></b> |
| 86 | + |
| 87 | +``` typescript |
| 88 | +//index.ts |
| 89 | +import CreateServer from "buxt"; |
| 90 | + |
| 91 | +const server = await CreateServer({ |
| 92 | + port: 3000, |
| 93 | + routeRoot: "api", |
| 94 | + cors: true, |
| 95 | + corsConfig: { |
| 96 | + origins: [ "*" ] |
| 97 | + } |
| 98 | +}); |
| 99 | +await server.listen(); |
| 100 | +``` |
| 101 | + |
| 102 | +### Definitions for config |
| 103 | + |
| 104 | +``` typescript |
| 105 | +type BuxtConfig = { |
| 106 | + port: number, |
| 107 | + routeRoot: string, |
| 108 | + cors?: boolean = false, |
| 109 | + corsConfig?: CorsConfig = null |
| 110 | +} |
| 111 | + |
| 112 | +type CorsConfig = { |
| 113 | + origins: string[], |
| 114 | + allowedMethods?: HttpMethod[] = ["GET", "OPTIONS", "POST"] |
| 115 | +} |
| 116 | + |
| 117 | +type HttpMethod = "GET" | "POST" | "PUT" | "DELETE" | "OPTIONS" | "HEAD" | "PATCH"; |
| 118 | + |
| 119 | +``` |
| 120 | + |
| 121 | +### <u><b>Route parameters</b></u> |
| 122 | +Route parameters work they they do in Next.js - they're denoted by a variable name surrounded by square brackets, eg: routes/user/[user].ts |
| 123 | + |
| 124 | +They can then be accessed on the BuxtRequest object under `req.routeParameters.{variable_name}` |
| 125 | +``` typescript |
| 126 | +//routes/user/[user].ts |
| 127 | +import type { BuxtRequest, BuxtResponse } from "buxt"; |
| 128 | + |
| 129 | +export default async function(req: BuxtRequest, res: BuxtResponse) { |
| 130 | + res.send("Hello " + req.routeParameters.user); |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +### <u><b>Enabling Cors</b></u> |
| 135 | +You must create a server using a config object to enable cors responses. |
| 136 | + |
| 137 | +``` typescript |
| 138 | +//index.ts |
| 139 | +import CreateServer from "buxt"; |
| 140 | + |
| 141 | +const server = await CreateServer({ |
| 142 | + port: 3000, |
| 143 | + routeRoot: "api", |
| 144 | + cors: true, |
| 145 | + corsConfig: { |
| 146 | + origins: [ "localhost:3000", "localhost:3001", "https://miaz.xyz/", "http://miaz.xyz" ], |
| 147 | + allowedMethods: [ "GET", "POST", "OPTIONS", "PUT", "DELETE"] |
| 148 | + } |
| 149 | +}); |
| 150 | +``` |
| 151 | + |
| 152 | +Firstly, make sure the cors key is set to `true`, then pass in a `CorsConfig` object. The `CorsConfig`'s `origins` key cannot be null. If you're allowing all origins then simply make it a single item array with `["*"]`. |
33 | 153 |
|
| 154 | +<b>Reminder</b> that you cannot combine wildcard routes and non-wildcard routes; if you attempt to do this then it will throw an error. |
34 | 155 |
|
35 | 156 | ---
|
36 |
| -##### Big thanks to <a href="https://github.com/lau1944">lau1994</a> and their project <a href="https://github.com/lau1944/bunrest">Bunrest</a> (really nice express-like server built for Bun) which has helped me a lot getting this project started. |
| 157 | +###### Big thanks to <a href="https://github.com/lau1944">lau1994</a> and their project <a href="https://github.com/lau1944/bunrest">Bunrest</a> (really nice express-like server built for Bun) which has helped me a lot getting this project started. |
0 commit comments