# Serve Kotlin/JS distribution as static assets in Kotlin/JVM project with Ktor

**URL:** <https://discuss.kotlinlang.org/t/serve-kotlin-js-distribution-as-static-assets-in-kotlin-jvm-project-with-ktor/17016>\
**Category:** Web Development\
**Created:** [April 7, 2020, 8:39pm UTC](https://discuss.kotlinlang.org/t/serve-kotlin-js-distribution-as-static-assets-in-kotlin-jvm-project-with-ktor/17016 "2020-04-07T20:39:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![axelfontaine](https://avatars.discourse-cdn.com/v4/letter/a/f0a364/32.png) [@axelfontaine](https://discuss.kotlinlang.org/u/axelfontaine)\
**Post date:** [April 7, 2020, 8:39pm UTC](https://discuss.kotlinlang.org/t/serve-kotlin-js-distribution-as-static-assets-in-kotlin-jvm-project-with-ktor/17016/1 "2020-04-07T20:39:42Z")

</div>

I have the following project structure:

MyProjectBuiltWithGradle  
|- MyClientKotlinJSModule  
|- MyServerKotlinJVMModuleWithKtor

What is the best way to take the assets produced by MyClientKotlinJSModule (resources + compiled JS file which includes my code and the Kotlin/JS runtime) and serve them as static content using Ktor as part of the MyServerKotlinJVMModuleWithKtor with either Kotlin 1.3.71 or 1.4-M1?

---

<div class="post-metadata">

**Author:** ![christofferberg79](https://avatars.discourse-cdn.com/v4/letter/c/c0e974/32.png) [@christofferberg79](https://discuss.kotlinlang.org/u/christofferberg79)\
**Post date:** [April 9, 2020, 5:41pm UTC](https://discuss.kotlinlang.org/t/serve-kotlin-js-distribution-as-static-assets-in-kotlin-jvm-project-with-ktor/17016/2 "2020-04-09T17:41:49Z")

</div>

I do something similar with Kotlin 1.3.71 in a multiplatform project with a JS frontend and a JVM backend with Ktor. During development I just run the backend on its own using a JavaExec task:

```auto
register<JavaExec>("jvmServerRun") {
    classpath = output.allOutputs + configurations["jvmRuntimeClasspath"]
    main = "io.ktor.server.netty.EngineMain"
}

```

and serve the frontend using Webpack DevServer with a proxy:

```auto
browser {
    runTask {
        devServer = devServer?.copy(
            port = 8088,
            proxy = mapOf("/api" to "http://localhost:8080")
        )
    }
}

```

When I want to deploy everything I want to serve the frontend from the Ktor server. To achieve this I copy the generated JS bundle and resources (index.html + source map) to a sub folder of the processed resources of the JVM distribution before I build the fat jar for the server:

```auto
    register<Copy>("copyJsBundleToKtor") {
        from("$distsDir")
        into("$buildDir/processedResources/jvm/main/web")
    }

    register("stage") {
        dependsOn("jsBrowserProductionWebpack", "copyJsBundleToKtor", "fatJar")
    }

    named("copyJsBundleToKtor") {
        mustRunAfter("jsBrowserProductionWebpack")
    }

    named("fatJar") {
        mustRunAfter("copyJsBundleToKtor")
    }

```

And then Ktor serves the JS parts using a static route:

```auto
routing {
    static("/") {
        resources("web")
        defaultResource("web/index.html")
    }

```

---

<div class="post-metadata">

**Author:** ![Raman.Gupta](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/raman.gupta/32/1834_2.png) [@Raman.Gupta](https://discuss.kotlinlang.org/u/Raman.Gupta)\
**Post date:** [March 7, 2023, 3:36pm UTC](https://discuss.kotlinlang.org/t/serve-kotlin-js-distribution-as-static-assets-in-kotlin-jvm-project-with-ktor/17016/3 "2023-03-07T15:36:03Z")

</div>

Sorry for bumping an old topic here, but this thread is surfaced by Google when searching for this topic, and I feel like an updated response would be useful.

An example of doing this is here: [GitHub - Kotlin/full-stack-web-jetbrains-night-sample: Full-stack demo application written with Kotlin MPP](https://github.com/Kotlin/full-stack-web-jetbrains-night-sample).

See specifically these lines in the client build:

> <https://github.com/Kotlin/full-stack-web-jetbrains-night-sample/blob/75e167ce1bc11f6ee776544c6f58b58db06f27be/client/build.gradle.kts#LL45-L52C2>

And these lines in the server build:

> <https://github.com/Kotlin/full-stack-web-jetbrains-night-sample/blob/master/server/build.gradle.kts#L17-L20>

and

> <https://github.com/Kotlin/full-stack-web-jetbrains-night-sample/blob/75e167ce1bc11f6ee776544c6f58b58db06f27be/server/build.gradle.kts#L41-L48>

and

> <https://github.com/Kotlin/full-stack-web-jetbrains-night-sample/blob/75e167ce1bc11f6ee776544c6f58b58db06f27be/server/build.gradle.kts#L55-L57>

However before using this as-is, note this issue I just created: [Security: Static client resources are loaded from the root package, giving access to the entire backend jar · Issue #15 · Kotlin/full-stack-web-jetbrains-night-sample · GitHub](https://github.com/Kotlin/full-stack-web-jetbrains-night-sample/issues/15).
