Using Different Servers with Svelte
Svelte by default uses the Node.JS http module as it's router. Here we will talk about using Express or Polka as middleware to implement API routes.
Assumptions
We won't go over installing Express or Polka here.
Using adapter-node
- Building SvelteKit for Node JS
To use an alternative Node.JS server first, you will need to build your application for Node using the
adapter-node
package. Follow the linked tutorial, then proceed.
Using Express
After building the application for node, Svelte will output two files, handler.js
and index.js
. The handler.js
file will be the file you pass into Express as middleware.
Next, make a new file and paste the following code into it. This import the handler.js
file into an express server and adds a /healthcheck
route then starts the server on port 3000
. If you built the application in a different directory make sure to change the ./build/handler.js
route.
1import { handler } from "./build/handler.js";
2import express from "express";
3
4const app = express();
5
6// add a route that lives separately from the SvelteKit app
7app.get("/healthcheck", (req, res) => {
8 res.end("ok");
9});
10
11// let SvelteKit handle everything else, including serving prerendered pages and static assets
12app.use(handler);
13
14app.listen(3000, () => {
15 console.log("listening on port 3000");
16});
Using Polka
Polka and Express have very simular APIs, which makes using them interchangeably very easy. To use polka simply replace the import statement for express with polka and everything will work.