https://kotlinlang.org logo
#ktor
Title
# ktor
d

Dominick

04/22/2021, 4:54 AM
How can I make it so I host the website & the backend in the ktor app. I was thinking of static content and then XMLHTTPRequest to access the backend api: ktor app +----------- / -> public/index.html (has assets that also need to be accessible from the /public folder) /security -> public/security.html (has assets that also need to be accessible from the /public folder) /api -> handled by the api using GET, POST, etc. functions How should my routes look? EDIT: I tried this but it didn't really work. /api returned correctly but / and /index.html or any other asset in /public did not show up.
Copy code
get("/api") {
            call.respondText("works")
        }

        static {
            resource("/", "public/index.html")
            resource("*", "public/index.html")
            resources("public")
        }
h

hhariri

04/22/2021, 5:25 AM
If you have a static website, then yes, you can just serve it as static or even use any kind of view engine. Not show up you mean how? 404?
d

Dominick

04/22/2021, 5:29 AM
@hhariri I want to serve the html with js and css in different files (all in /public) at the root of the website, then override /api and /file to be handled by get("/api") etc etc
h

hhariri

04/22/2021, 5:29 AM
That should work with a single entry for your route to resources. You don’t need to do further mappings in static.
d

Dominick

04/22/2021, 5:30 AM
Can you give me an example Route for that? I have tried many things and they don't work for me
h

hhariri

04/22/2021, 5:31 AM
These should all be correct and working https://ktor.io/docs/serving-static-content.html#folders. Shows how to also distinguish js/css. Click on the demo project at the top.
d

Dominick

04/22/2021, 5:31 AM
I have tried those already, that's the page I started at. I get 404 when trying root, domain.com/index.html etc
h

hhariri

04/22/2021, 6:15 AM
DM me a sample that doesnt work.