-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
195 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,111 @@ | ||
# Add to existing Vite app | ||
|
||
(Coming Soon) | ||
While vite is amazing as a devkit for client-side web applications, it requires some setup to use the server in the same application. The funny part is that there is a server running both in dev and prod, its just that you don't get access to it. | ||
|
||
Vinxi gives you the ability to add server-side functionality (SSR) to an existing Vite app. | ||
|
||
A typical `vite` app is a single-page application, with an `index.html` file, a `public` directory and a `vite.config.ts` file. | ||
|
||
Lets install `vinxi` to get started: | ||
|
||
```bash [npm] | ||
npm install vinxi | ||
``` | ||
|
||
The first step is to make your `vite` app into a `vinxi` app. You don't need to create any new files. Vinxi works with `vite.config.ts` files too. You just need to change what you export from the file. | ||
|
||
Here is an example of a `vite.config.ts` file that exports the same `vite` app as a `vinxi` app: | ||
|
||
```ts twoslash | ||
import { createApp } from "vinxi"; | ||
import { config } from "vinxi/plugins/config"; | ||
|
||
export default createApp({ | ||
routers: [ | ||
{ | ||
name: "public", | ||
type: "static", | ||
dir: "./public", | ||
}, | ||
{ | ||
name: "client", | ||
type: "spa", | ||
handler: "./index.html", | ||
base: "/", | ||
plugins: () => [ | ||
config("custom", { | ||
// additional vite options | ||
}), | ||
// additional vite plugins | ||
], | ||
}, | ||
], | ||
}); | ||
``` | ||
|
||
Now running `npm run dev` will start the Vinxi dev server and you will get the same experience as you did with `vite`. | ||
|
||
The only difference is that you can now add server-side functionality to your app, and much more. Let's see how. | ||
|
||
Let's imagine we need to send an email when a user submits a form. We can use the `nodemailer` package to do that. Let's install it. | ||
|
||
```bash[npm] | ||
npm install nodemailer | ||
``` | ||
|
||
```bash[yarn] | ||
yarn add nodemailer | ||
``` | ||
|
||
```bash[pnpm] | ||
pnpm add nodemailer | ||
``` | ||
|
||
But `nodemailer` is not something you can use in the browser. You need to run it in `node` (on the server). So we need some kind of API routes. In `vinxi`, you can add a server handler for an API to your app. | ||
|
||
```ts | ||
import { createApp } from "vinxi"; | ||
|
||
export default createApp({ | ||
routers: [ | ||
{ | ||
name: "public", | ||
type: "static", | ||
dir: "./public", | ||
}, | ||
{ | ||
name: "client", | ||
type: "spa", | ||
handler: "./index.html", | ||
base: "/", | ||
plugins: () => [ | ||
config("custom", { | ||
// additional vite options | ||
}), | ||
// additional vite plugins | ||
], | ||
}, | ||
{ | ||
name: "api", | ||
type: "http", | ||
handler: "./api.ts", | ||
base: "/api", | ||
}, | ||
], | ||
}); | ||
``` | ||
|
||
```ts [api.ts] | ||
import nodemailer from "nodemailer"; | ||
import { eventHandler } from "vinxi/http"; | ||
|
||
export default eventHandler(async (event) => { | ||
await nodemailer.sendMail({ | ||
from: "", | ||
}); | ||
|
||
return "done"; | ||
}); | ||
``` | ||
|
||
You can now hit `http://localhost:3000/api` from your frontend to send an email. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters