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.
We won't go over installing Express or Polka here.
- 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-nodepackage. Follow the linked tutorial, then proceed.
After building the application for node, Svelte will output two files,
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
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.