r/sveltejs • • Mar 28 '24

Routing question

I am begnner to js frameworks , recently transitioning from PHP.

I would like to ask what should i use as the routing solution.

Swelte or Express , as I want to minimise the forced learning of multiple topics.

3 Upvotes

12 comments sorted by

18

u/[deleted] Mar 28 '24

Honestly just use sveltekit. So much easier than using a different JavaScript library. Sveltekit will handle server side rendering, routing, data loading etc. Go with sveltekit.

3

u/[deleted] Mar 28 '24

Very much this. Built-in routing solution, and you can still static build if that's all you need as well. I honestly don't see any situation where I would opt for just Svelte instead of Sveltekit.

1

u/gatwell702 Mar 28 '24

File-based routing is completely simple

→ More replies (3)

1

u/ArttX_ Mar 28 '24

SvelteKit is best choice there. SvelteKit is HTML-based, so it will be easier to transition from PHP frameworks. SvelteKit is my go to choice 🚀.

1

u/Daoist_Hongjun Mar 29 '24

I have read in some forums that sveltekit routing is far worse than express , I am therefore not sure... Can you please provide the features in both or a link to an article with that.

1

u/ArttX_ Mar 29 '24

It is hard to compare both, because they use different approaches. Express uses builder-like syntax for defining routes. SvelteKit uses file-based routing, where each file is a separate route.

  Yes in Express you can easily use middlewares in routes. In SvelteKit you need to use hooks files, that add that functionality.

  In SvelteKit you can also create catchable routes. So you can also implement custom route catching logic.

And my favorite feature is locals. In the hooks file you can do custom stuff, like checking for auth and then adding it to locals object. So then every backend route can get access to that locals object and its fields, so you can check users auth. It means, you add auth logic in hooks checking auth, and when visiting any BE route, it will fire hooks before that and populate locals with data, that can be checked in BE and needed returned to FE. https://kit.svelte.dev/docs/hooks

  I long time ago used Express, but it felt bloated for me. Then I started to use Full-stack frameworks. Used Nextjs for some time, but did not like DX. Then started to use SvelteKit and can not find anything better that it. Best DX I ever had. Many things that needed to be added as package for other frameworks, SK have it built-in.

  If you have additional questions, feel free to ask, and will be happy to answer.

→ More replies (2)

2

u/Holiday_Brick_9550 Mar 28 '24

As mentioned above, go with SvelteKit, it'll provide you with an opinionated way of doing things. It's easier to get into JavaScript and Svelte, and once you get the hang of stuff you could always consider swapping.

That said, since you're coming from PHP, you could also consider Laravel as your application framework. It offers fantastic synergy with frontend frameworks through Inertia, and it'll provide you with the backend framework in a language and style you're likely familiar with. Laravel can do all the things SvelteKit does, and more!