Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Husky Routing #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
bun.lockb
15 changes: 15 additions & 0 deletions husky/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Husky with Bun runtime

## Getting Started
To get started with this template, simply paste this command into your terminal:
```bash
bun create husky ./husky-example
```

## Development
To start the development server run:
```bash
bun run dev
```

Open http://localhost:3000/ with your browser to see the result.
33 changes: 33 additions & 0 deletions husky/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Husky, Output } from "husky-routing";

/*
The Husky class is the entry point of the application.
Here you can start the server and configure it.
*/
const app = new Husky({
port: 3000,
logging: {
allowError: true,
allowInfo: true,
allowHTTP: false,
allowWS: false
}
});

/*
You can add routers to the application by calling the use method on the Husky class.
For best practices, you should create a separate file for each router.
*/
import indexRouter from "./routes/index.route";
app.use(indexRouter);


/*
You can start the application by calling the start method.
In addition, you can pass a callback function that will be called when the server is started.
*/
app.start({
callback: (port) => {
Output.info(`Server started on port ${port}`);
},
});
19 changes: 19 additions & 0 deletions husky/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"name": "@bun-examples/create-husky",
"version": "1.0.0",
"description": "Create a new Husky project using 'bun create husky'",
"dependencies": {
"husky-routing": "latest"
},
"devDependencies": {
"bun-types": "latest"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "bun run --watch index.ts"
},
"module": "index.ts",
"bun-create": {
"start": "bun run index.ts"
}
}
27 changes: 27 additions & 0 deletions husky/routes/index.route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Router } from "husky-routing";

/*
You can add router to the application by creating a new Router.
The first parameter is the router path.
The router supports function nesting.
*/
const router = new Router("/");

/*
You can add routes to the router by calling the get method.
The first parameter is the route path. Please note that this path is relative to the router path.
The second parameter is the route handler.
The route handler is a function that takes two parameters:
- req: The request object.
- params: The route parameters.
The route handler must return a Response or a Promise<Response> object.
*/
router.get("/", (req) => {
return new Response("Hello World!");
});

router.get("/name/:name", (req, params) => {
return new Response(`Hello ${params.name}!`);
});

export default router;