htmv 0.0.9 → 0.0.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +40 -61
- package/package.json +1 -1
- package/src/cli/commands/new.ts +3 -0
package/README.md
CHANGED
|
@@ -1,64 +1,26 @@
|
|
|
1
1
|
# HTMV
|
|
2
2
|
HTMV. A simple yet fast web framework currently in work in progress. Installation and usage guide can be found below.
|
|
3
3
|
|
|
4
|
+
# Precautions
|
|
5
|
+
HTMV currently only works on Bun. Routing on Node.js is broken. Please take note and only use Bun with it while I'm working on Node.js support.
|
|
6
|
+
|
|
4
7
|
# Installation
|
|
5
|
-
|
|
6
|
-
1. Leave that folder and create a new folder for your project
|
|
7
|
-
```bash
|
|
8
|
-
$ cd .. # or cd into wherever you'd like to make your project's folder
|
|
9
|
-
$ mkdir my-first-htmv-project
|
|
10
|
-
```
|
|
11
|
-
2. Create an empty bun project and add HTMV as a dependency
|
|
8
|
+
It's simple! Just use htmv's CLI!
|
|
12
9
|
```bash
|
|
13
|
-
|
|
14
|
-
bun add htmv # Adds HTMV dependency
|
|
15
|
-
```
|
|
16
|
-
3. Create two folders. One for routes and the other for views (You can choose any name)
|
|
17
|
-
4. Now in the `index.ts` add this
|
|
18
|
-
```ts
|
|
19
|
-
import path from "node:path";
|
|
20
|
-
import { setup } from "htmv";
|
|
21
|
-
|
|
22
|
-
const dirPath = import.meta.dir;
|
|
23
|
-
setup({
|
|
24
|
-
routes: path.join(dirPath, "routes"), // Change to your routes folder name
|
|
25
|
-
views: path.join(dirPath, "views"), // Change to your views folder name
|
|
26
|
-
port: 3000,
|
|
27
|
-
});
|
|
28
|
-
```
|
|
29
|
-
5. Let's create your first view! Inside the views folder add a new `example.html` with the contents
|
|
30
|
-
```html
|
|
31
|
-
<h1>Welcome to my blog!</h1>
|
|
32
|
-
<p>{description}</p>
|
|
33
|
-
```
|
|
34
|
-
6. Finally, let's create a route for it. HTMV has file based routing. Every folder inside your `routes` folder indicates a nested route and every `index.ts` is it's entry point. For now, let's keep it simple with just one `index.ts` in it. Create the file and add the next contents
|
|
35
|
-
```ts
|
|
36
|
-
import { type RouteParams, render } from "htmv";
|
|
37
|
-
|
|
38
|
-
export default async function MyRoute(_params: RouteParams) {
|
|
39
|
-
return await render("example", {
|
|
40
|
-
description: "This is my first time using HTMV ❤️",
|
|
41
|
-
});
|
|
42
|
-
}
|
|
10
|
+
bunx htmv@latest new my_cool_project
|
|
43
11
|
```
|
|
12
|
+
This will create an htmv project on the folder `my_cool_project`. Finally `cd` into it to get started. Dependencies are already installed. No need for a `bun install`.
|
|
44
13
|
|
|
45
14
|
## And that's it! We're done.
|
|
46
|
-
Now let's try it! You can simply start it with `bun
|
|
15
|
+
Now let's try it! You can simply start it with `bun dev`. After that, you should now be able to see your page in `http://localhost:3000`.
|
|
47
16
|
|
|
48
17
|
## Final note
|
|
49
|
-
Did you see how the `{
|
|
18
|
+
Did you see how the `{title}` value on our view changed to the one we gave it on our route? Now that's where HTMV gets fun! Just as we now did you could also do more complex stuff like access your DB's data and show it in a nicely form.
|
|
50
19
|
|
|
51
20
|
# Static files
|
|
52
21
|
Having your views is nice and all... but what about images? videos? or maybe you would like to not have to use `<style>` for CSS and `<script>` for your JS.
|
|
53
22
|
|
|
54
|
-
|
|
55
|
-
```ts
|
|
56
|
-
setup({
|
|
57
|
-
//... the other folders
|
|
58
|
-
public: path.join(dirPath, 'public')
|
|
59
|
-
})
|
|
60
|
-
```
|
|
61
|
-
You can add any **static files** onto this folder and they will be automatically served at `/public`. Take that into account when importing them inside your `view`.
|
|
23
|
+
For this, you can just add any **static files** onto the `public` folder and they will be automatically served at `/public`. Take that into account when importing them inside your `view`.
|
|
62
24
|
|
|
63
25
|
# Dynamic routes
|
|
64
26
|
Sometimes you don't know the exact route (this is more common in `APIs`). For example, let's say you want to have a route `/api/user/USER_ID_HERE`. Of course you don't want to have a million folders every single one named with a different user id. That's where dynamic routes come into play.
|
|
@@ -76,19 +38,36 @@ export default function UserEndpoint(routeParams: RouteParams) {
|
|
|
76
38
|
```
|
|
77
39
|
Now when you go to `/api/user/1000` you should see `Hello user 1000!`.
|
|
78
40
|
|
|
79
|
-
#
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
41
|
+
# Route handlers
|
|
42
|
+
Following with `APIs`, you sometimes want a single endpoint for your users, for example `/api/user` and depending on the method used on the request act accordingly. For example, create an user with method `POST`, get users with method `GET` and more.
|
|
43
|
+
|
|
44
|
+
Normally you'd do that programatically like the following:
|
|
45
|
+
```ts
|
|
46
|
+
import { type RouteParams } from "htmv";
|
|
47
|
+
|
|
48
|
+
export default function UserEndpoint(routeParams: RouteParams) {
|
|
49
|
+
const method = routeParams.request.method
|
|
50
|
+
if(method === "GET") {
|
|
51
|
+
// list users
|
|
52
|
+
}
|
|
53
|
+
if(method === "POST") {
|
|
54
|
+
// create user
|
|
55
|
+
}
|
|
92
56
|
}
|
|
93
57
|
```
|
|
94
|
-
|
|
58
|
+
Route handlers allow you to this in an easier way. Just have functions for each method!
|
|
59
|
+
```ts
|
|
60
|
+
import { type RouteParams } from "htmv";
|
|
61
|
+
|
|
62
|
+
export function GET(routeParams: RouteParams) {
|
|
63
|
+
// list users
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export function POST(routeParams: RouteParams) {
|
|
67
|
+
// create user
|
|
68
|
+
}
|
|
69
|
+
```
|
|
70
|
+
Note how the `default` keyword was removed, that keyword is instead reserved for when you want to hit all endpoints (`ALL` method).
|
|
71
|
+
|
|
72
|
+
# Hot reloading
|
|
73
|
+
Having to restart the server every time you make a change can be quite tedious. HTMV takes care of this thanks to Bun. Just develop with `bun dev` and it should work out of the box! Note that this does not include hot reloading in the browser. As of now, you have to refresh the page to see new changes. It doesn't update in real time.
|
package/package.json
CHANGED
package/src/cli/commands/new.ts
CHANGED
|
@@ -58,6 +58,9 @@ export default async (_params: RouteParams) => {
|
|
|
58
58
|
</body>
|
|
59
59
|
</html>`;
|
|
60
60
|
await fs.writeFile(path.join(fullPath, "views", "example.html"), viewContent);
|
|
61
|
+
console.log("5. Creating run scripts...");
|
|
62
|
+
await runCommand(`npm pkg set scripts.dev="bun --watch ."`);
|
|
63
|
+
await runCommand(`npm pkg set scripts.start="bun index.ts"`);
|
|
61
64
|
console.log(`All done! Project ${name} created.`);
|
|
62
65
|
console.log(`Now run cd ${name} and start building your next big project!`);
|
|
63
66
|
};
|