# SvelteKit HTTP Server

import { Tabs, TabsContent, TabsList, TabsTrigger } from "zudoku/ui/Tabs"

{/* vale off */}
{/* THIS FILE WAS AUTOGENERATED FROM THE PUBLIC EXAMPLE REPOSITORY. DO NOT EDIT THIS FILE DIRECTLY. */}


This guide explains how to create and deploy a [SvelteKit](https://kit.svelte.dev/) app.
[SvelteKit](https://kit.svelte.dev/) builds on Svelte, a UI framework that uses a compiler to let you write breathtakingly concise components that do minimal work in the browser.

To run this example, follow these steps:

1. Install the [unikraft CLI](/cli).
   You need a [BuildKit](https://github.com/moby/buildkit) builder. The easiest way to get one is via [Docker](https://docs.docker.com/engine/install/).
   Alternatively, you can also directly set up and use BuildKit, see the [quick start](https://github.com/moby/buildkit#quick-start).

2. Clone the [`examples` repository](https://github.com/unikraft-cloud/examples) and `cd` into the `examples/httpserver-node22-sveltekit/` directory:

   ```bash
   git clone https://github.com/unikraft-cloud/examples
   cd examples/httpserver-node22-sveltekit/
   ```

Make sure to log into Unikraft Cloud and pick a [metro](/platform/metros) close to you.
This guide uses `fra` (Frankfurt, 🇩🇪):

```bash
unikraft login
```

When done, invoke the following command to deploy this app on Unikraft Cloud:

```bash
unikraft build . --output <my-org>/httpserver-node22-sveltekit:latest
unikraft run --metro fra \
  -m 512M \
  -p 443:3000/tls+http \
  --scale-to-zero policy=on,cooldown-time=1000 \
  --image <my-org>/httpserver-node22-sveltekit:latest
```

The output shows the instance address and other details:

```ansi
[1mmetro[22m:        fra
[1mname[22m:         httpserver-node22-sveltekit-zmt39
[1muuid[22m:         cd5071b0-5605-4771-b75d-4789393e60de
[1mstate[22m:        [38;2;144;161;185mstarting[0m
[1mimage[22m:        <my-org>/httpserver-node22-sveltekit
[1mresources[22m:
[3m  memory[23m:     512MiB
[3m  vcpus[23m:      1
[1mservice[22m:
[3m  uuid[23m:       2cd097b5-795c-7184-2968-1508c630fb2b
[3m  name[23m:       dark-fog-z18n0ej1
[3m  domains[23m:
[3m  - fqdn[23m:     dark-fog-z18n0ej1.fra.unikraft.app
[1mnetworks[22m:
[3m- uuid[23m:       e715723d-4659-2006-bd7b-68a5a3c33cce
[3m  private-ip[23m: 10.0.3.3
[3m  mac[23m:        12:b0:a0:31:90:5a
[1mtimestamps[22m:
[3m  created[23m:    just now
```

In this case, the instance name is `httpserver-node22-sveltekit-zmt39` and the address is `https://dark-fog-z18n0ej1.fra.unikraft.app`.
They're different for each run.

Use `curl` to query the Unikraft Cloud instance:

```bash
curl https://dark-fog-z18n0ej1.fra.unikraft.app
```

```text
<!doctype html>
<html lang="en">
[...]
        <body data-sveltekit-preload-data="hover">
[...]
```

Or even better, point a browser at it 😀
This will get you to play with the SvelteKit demo app.

You can list information about the instance by running:

```bash
unikraft instances list
```

```ansi
[1mMETRO[22m  [1mNAME[22m                               [1mSTATE[22m    [1mIMAGE[22m                                 [1mARGS[22m  [1mMEMORY[22m  [1mVCPUS[22m  [1mFQDN[22m                                [1mCREATED[22m
fra    httpserver-node22-sveltekit-zmt39  [38;2;0;188;125mrunning[0m  <my-org>/httpserver-node22-sveltekit        512MiB  1      dark-fog-z18n0ej1.fra.unikraft.app  2 minutes ago
```

When done, you can remove the instance:

```bash
unikraft instances delete httpserver-node22-sveltekit-zmt39
```

## Customize your app

To customize the app, update the files in the repository.
That is, you update the SvelteKit / Node / `npm`-specific files, or the Unikraft Cloud-specific files.
This section details each below.

### Updating the app

Updating the SvelteKit app is reliant on making `npm`-related updates.

[`npm`](https://www.npmjs.com/) is a package manager for Node.
It's used to install dependencies for Node apps.
`npm` uses a `package.json` file to list required dependencies (with versions).

[`npm create svelte@latest`](https://kit.svelte.dev/docs/creating-a-project) generated the app files, using the [`@sveltejs/adapter-node`](https://kit.svelte.dev/docs/adapter-node) adapter.

The `src/routes/sverdle/` directory contains the core implementation.
Detailed information on updating the implementation is part of the [official SvelteKit documentation](https://kit.svelte.dev/docs/introduction).

Updates to the implementation will probably require updates to the `package.json` file.
The `package.json` file lists [`npm` app dependencies](https://docs.npmjs.com/cli/v8/configuring-npm/package-json#dependencies).

#### Deploying locally

Before deploying the SvelteKit app on Unikraft Cloud, you can deploy locally.
Assuming `npm` is installed, use the following commands:

```bash
npm install
npm run dev
```

```text
  VITE v5.2.6  ready in 807 ms

  ➜  Local:   http://localhost:5173/
  ➜  Network: use --host to expose
  ➜  press h + enter to show help
```

You can test the app locally by pointing your browser to `http://localhost:5173/`.

### Sample specification

Lines in the `Kraftfile` have the following roles:

* `spec: v0.7`: The current `Kraftfile` specification version is `0.7`.

* `runtime: base-compat:latest`: The runtime kernel to use is the base compatibility kernel.

* `rootfs`: Build the app root filesystem.
  `source: ./Dockerfile` means the filesystem is built using the `Dockerfile`.
  `format: erofs` means the filesystem type is [EROFS](https://erofs.docs.kernel.org/).

* `cmd: [["/usr/bin/node", "/app/build/index.js"]]`: Use `/usr/bin/node /app/build/index.js` as the starting command of the instance.

Lines in the `Dockerfile` have the following roles:

* `FROM node:21-alpine AS deps`: Use the base image of the `node:21-alpine` container.
  This provides the `npm` binary and other Node-related components.
  Name the current image `deps`.

* `WORKDIR /app`: Use `/app` as working directory.
  All other commands in the `Dockerfile` run inside this directory.

* `COPY package.json /app`: Copy the `npm` configuration file to be able to install dependencies.

* `RUN npm install`: Install `npm` components listed in `packages.json`.

* `FROM deps AS build`: Create a fresh checkpoint of the base image for the build phase.

* `COPY . /app`: Copy contents of the current directory (the actual app files) in the Docker filesystem.
  Note that paths in the `.dockerignore` file aren't copied.

* `RUN npm run build; ...`: Build the files required for running the app standalone.
  The app entry point is the resulting `/app/build/index.js` file that's used as the start command in the `Kraftfile`.
  To run the app, configure it as a module.

* `FROM scratch as prod`: Use [the `scratch` empty filesystem`](https://hub.docker.com/_/scratch/) as the actual runtime environment.
  It will only contain the required files from the build.

* `COPY --from=build ...`: Copy existing files from new `build` image to the `scratch`-based image.
  This means the app build directory (`/app/build`) and the required dependencies (`/app/node_modules`).

#### Customization options

It's unlikely you will have to update the `Kraftfile`.
The only potential update is the `cmd` option.
But since SvelteKit / `npm` generates the `/app/build/index.js` file, you're unlikely to update it.

You also don't need to change the `Dockerfile`.
Updates to the app would be part of the `package.json` file or the `src/` directory.
And updating these won't affect the contents of the `Dockerfile`.
Still, if required, apps may require extending the `Dockerfile` with extra [`Dockerfile` commands](https://docs.docker.com/engine/reference/builder/).

## Learn more

Use the `--help` option for detailed information on using Unikraft Cloud:

```bash
unikraft --help
```

Or visit the [CLI Reference](/cli/unikraft).
{/* vale on */}
