Skip to content

Commit

Permalink
updated docs / added routing docs
Browse files Browse the repository at this point in the history
  • Loading branch information
floodfx committed Feb 18, 2022
1 parent 2e7324f commit 80202a8
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ lvServer.start();

### Additional Feature Documentation
* [Updating HTML Document `<title />`](docs/updating-html-title.md)
* [LiveViewJS Changesets](docs/changesets.md).
* [Routing Details](docs/routing.md).

#### Other features to be implemented:
* [LiveView Helpers](https://hexdocs.pm/phoenix_live_view/Phoenix.LiveView.Helpers.html) - Vote for [Issue 17](https://github.com/floodfx/liveviewjs/issues/17)
Expand Down
43 changes: 43 additions & 0 deletions docs/routing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
## Routing

LiveViewJS supports routing to both `LiveViewComponent` and traditional web route handlers.

As noted in the examples, the `LiveViewRouter` is a simple object that maps paths to `LiveViewComponent`s.
```ts
// import package
import {LiveViewServer} from "liveviewjs";

// create new LiveViewServer
const lvServer = new LiveViewServer();

// define your routes by mapping paths to LiveViewComponents
const lvRouter: LiveViewRouter = {
"/light": new LightLiveViewComponent();
}
// AND then passing the router to the server
lvServer.registerLiveViewRoutes(lvRouter);

// OR register your route with the server directly
// lvServer.registerLiveViewRoute("/light", new LightLiveViewComponent());
```

HTTP GET requests to `/light` route will be intercepted by the `LiveViewServer` and routed to the `LightLiveViewComponent`.

If you need to handle non-LiveView requests (for example, login routes, you can get the `expressApp` from the `LiveViewServer` and add route handlers to it in the same way you would with a traditional express app.

E.g. to register the `/foo` route:
```ts
lvServer.expressApp.get("/foo", (req, res) => {
// my business logic
res.send("Foo!")
})
```

Then when your `LiveViewServer` is started, it will handle both LiveView and non-LiveView requests.

```ts
// start server
lvServer.start();
```

See `src/examples/index.ts` for a working example.

0 comments on commit 80202a8

Please sign in to comment.