Hello All! I’ve got an issue running a JS script f...
# ktor
k
Hello All! I’ve got an issue running a JS script for a route in #ktor . Indeed, whatever syntax I use or whater location I put the static files, I cannot manage to get anything else than a 404 not found error when refreshing the page. Is there anything obvious that I do wrong? Thanks very much for your help !
s
The only problem I can see is that you are trying to load a file from the path
static/script.js
, but you file is really located on
files.static.js/script.js
c
It should be in resources root, not source root
k
@SackCastellon I had tried that address as well. And a lot of other combinations. But no luck...
@cy Not sure what you meant... Could you please guide me through the right doc?
@cy Got it! Thank you... Sorry for the question, it might be trivial but I did not know that way to handle js files. Thank you again for answering @cy and @SackCastellon!
r
To maybe help your understanding a little here, you have to put JS files into the resources folder, because you'er working in a Kotlin/JVM Project, and JS can't be run on the JVM (technically, they could, but that's another topic). They are static files delivered to your browser, and the fact that they contain executable code is only relevant once they are there and interpreted by your browser. To your kotlin code, they aren't really any different than images or css files.
k
Thank you @robin! It does help improving my understanding. So in your opinion, what would be the best architecture if I want the most of the flexibility of JS and still relying on Ktor (which I happened to love)? Should I keep that architecture (but I am afraid that those JS static files will bring limitation) or should I completely create a new API written in nodeJS communicating with Ktor (which is ... a lot to do)?
r
What do you mean by "flexibility of JS"? What are you doing inside the JS file, and why?
k
Well, eventually I would like to be able to add some nice JS capabilities like React components that could exchange with my ktor server.
r
Gotcha. So, basically, your backend (ktor) and frontend (html/css/js) code are more or less separate things. Right now, you're using the HTML DSL in Ktor, which blurs the lines a good bit as it's written directly inside the rest of your server code, but once your frontend becomes a little more complex it's a good practice the cleanly separate the frontend and backend code. Once you've done that, you're free to decide how those should work together - since any communication between frontend and backend will run over plain old http, your ktor server doesn't care in what framework and language the frontend is written. You could keep using the html dsl with some simple js files for behavior, all in the same project, or you could switch to any other Frontend Framework you'd like and even factor that out into it's own project. There's Kotlin/React, plain React, Vue, Angular, Elm, Svelte - Ktor doesn't really care at that point. To give you an example: I personally am working on an application that consists of a server written in Ktor and a separate Frontend which is written in Angular 9. They are separate projects that have no knowledge of each other. In our case the frontend is served statically from a separate Nginx server, but it could just as well be served by the Ktor server. I'm pretty happy with this setup, except I'd probably go with Kotlin/React instead of Angular if I were to start over.
k
Thank you very much for your insight. It makes sense for me. I've started to learn Kotlin recently (and without Java background, it's not that easy). A colleague of mine advised me to go directly to NodeJS if the goal is to do webapps but Kotlin is so great to manipulate. I'll then check what the best way to have both JS and Kotlin interacting with each other.