Ross
Ross

Reputation: 161

How do I connect an ExpressJS backend to a SvelteKit frontend?

I'm trying to create an app that uses ExpressJS as the backend and SvelteKit as the frontend. Express will be the entry point for the web server, so for example /api/ requests will be routed to ExpressJS and anything other than this will be sent to the SvelteKit frontend.

I'm using the node adaptor for SvelteKit and have found that it makes a fully contained node server in '/build/index.js' for the SvelteKit application.

However, this is where I'm stuck now and I'm not sure how to route the requests to the frontend through the Express app. Any help would be much appreciated. I'd ideally like to have this contained within one project.

Link to the node adaptor: https://github.com/sveltejs/kit/blob/master/packages/adapter-node/README.md

Upvotes: 5

Views: 6197

Answers (2)

Gouda P
Gouda P

Reputation: 126

This is the perfect answer but sometime if you new to express and svelte, then always use app.use(handler) after express routes defined. Otherwise express routes wont work.

EX - You might get errors like this, i posted because it might save some people time.

https://i.sstatic.net/mLDTv.png

Upvotes: 2

Ross
Ross

Reputation: 161

I think I worked it out! These two lines solve this in the express app:

import { handler } from './build/handler.js';
app.use(handler);

Upvotes: 11

Related Questions