Svelte + Svelte Router + Bootstrap + Express + dotenv
This package purpose is to provide a Svelte app template and an Express API bundled together. The Svelte application is not SSR.
Well, first, Svelte is the most amazing JS framework I ever used for a frontend application.
My aim was to be able to bundle into a single package the front and backend application, with the same environment.
In my use case, SSR was not convenient because I needed to make several calls to a backend API into the same page. I have considered to use Sapper, which is also amazing, was is not enough open for now, as I needed to manipulate routes, handle POST requests on the server, handling forms etc ...
Use npx to create a new application.
npx degit jlefebure/svelte-express-template my-app
Once done, install all dependencies
npm install
Run your new application with the following command
npm run dev
By default, the app is running on http://0.0.0.0:3000. This behaviour can be configured with environment variables (see bellow).
The template package is organized by separating frontend and server applications.
.
├── rollup.config.js
├── src
| ├── client
| └── server
| └── app.mjs
└── settings.mjs
client
folder contains sources of the Svelte applicationserver
folder contains sources of the Express applicationapp.mjs
start the Express server that will start the Express app and expose the public folder where the Svelte app is
compiled by defaultsettings.mjs
contains the settings of the application for both front and back applications.
├── client
| ├── components
| ├── pages
| ├── services
| ├── stores
| ├── main.js
| ├── App.svelte
components
contains all commons components of the client apppages
contains all pages defined set in the routerservices
contains exposed functions to call the Express serverstores
contains the exposed stores for the whole appmain.js
is the start point of the client appApp.svelte
contains the router, navbar, and is the main component of the app.
├── server
| ├── routes
| ├── index.mjs
Contains just a single file: the exposed endpoints.
Configuration of the application is handled with dotenv and dotenv-expand. Configuration is available for the Express and Svelte application. Because the Svelte application is not SSR, env variables are injected when Rollup is launched and after every compilation.
Configuration is done with dotenv with an embedded .env
file. You can either edit this file or override this
configuration by setting an environment variable.
The keys bellow are mandatory and must be configured.
Variable | Default value | Description |
---|---|---|
SVELTE_PORT | 3000 | Port on which the app and the API is exposed. |
SVELTE_EXTERNAL_URL | http://0.0.0.0:${SVELTE_PORT} | URL on which the application is exposed. This value is by the client to fetch data from the API. |
SVELTE_API_BASE_URL | /api | Base URL on which the server API is exposed. Trailing slash is deleted if present. |
SVELTE_CLIENT_BASE_URL | / | Base URL on which the frontend client is exposed. Trailing slash is deleted if present. |
You can override this configuration by setting an environment variable. The following example set the default port to 5000.
export SVELTE_PORT=5000
You can define your own variables by completing the .env
file. The configuration must follow two rules :
SVELTE_
Variables defined directly on the environment are also taken in consideration if it follow these two rules.
Because Svelte app is not SSR, variables are directly injected by the rollup-replace-plugin. To avoid ambiguous syntax,
the key must be respect the following format. For the SVELTE_PORT
variable :
%SVELTE_PORT%
These variables can be used everywhere in the application, mostly in strings values. Fetch calls that are good examples that you can follow. This call will be compiled from
return fetch(`%SVELTE_EXTERNAL_URL%%SVELTE_API_BASE_URL%/hello`, {
headers: {
"Content-Type": "application/json"
},
})
to
return fetch(`http://0.0.0.0:3000/api/hello`, {
headers: {
"Content-Type": "application/json"
},
})
Because the Express application is ... a server application, you can directly use variables in your code.
import settings from "./settings.mjs";
//value is "/"
let baseUrl = settings.SVELTE_CLIENT_BASE_URL
settings.mjs
must be importedThe Rollup configuration will compiled the Svelte application into public
folder and start the express server. Nothing
exotic here.
You can either deploy your future application in standalone mode or as a Docker container.
Just take all your project and run the following commands, to install depdencies, build and run your project.
npm install
npm run build
npm start
A Dockerfile is provided with the template app. It will take your project, install dependencies, and run the application
To build your image, in your project folder
docker build . -t myapp
To run your application
docker run -p 3000:3000 myapp
To push it on a distant repository (considering your are logged on the Docker registry)
docker push myapp