Creating a Website with Ktor (Part 1: Setting Up)

Exploring Ktor Projects

Michihiro Iwasaki
5 min readApr 4, 2024

When you think of Kotlin, what comes to mind? Many would say it’s a go-to language for Android app development — and they’d be right. But Kotlin’s capabilities extend far beyond mobile apps. With Kotlin, you can also craft web applications and websites, not just the front-end but also the back-end, similar to how you might use Java.

Enter Ktor, a Kotlin framework designed to streamline back-end development. With Ktor, creating the server-side of your app becomes more straightforward, allowing you to focus on the front-end aspects like HTML, CSS, and JavaScript. There’s no need to juggle multiple programming languages; Kotlin can handle it all.

This series will guide you through the process of building a website with Ktor. In this first article, we’ll cover the initial steps: setting up your Ktor project. Let’s embark on this journey to harness the full potential of Kotlin in web development.

✅ Prerequisites

Before diving into this tutorial, ensure you meet the following requirements:

  • Java JDK is installed (necessary if you’re using Fleet).
  • An Integrated Development Environment (IDE) like IntelliJ IDEA or Fleet is installed.

To develop a website using Ktor with Fleet, having Java on your PC is crucial. Setting up the development environment might take some time, but the versatility Fleet offers, supporting numerous programming languages out of the box, is a significant advantage. In this tutorial, we’ll focus on initiating a Ktor project in Fleet.

If you’re new to Fleet or Java JDK, I’ve covered their installation in a previous article. Check it out for detailed guidance: Executing Kotlin Code with Fleet

Additionally, Fleet’s official website offers a wealth of resources and support: Fleet — JetBrains

Once you’ve verified these prerequisites, we’re ready to embark on setting up our Ktor project!

📝 Creating Project Files with the Ktor Project Generator

Setting up a new Ktor project is a breeze with the Ktor Project Generator available on the Ktor website. Here’s how to use it:

1- Access the Ktor Project Generator: Navigate to Ktor’s official website and click on “Create” in the top menu bar. You’ll be directed to a page like the one shown in the image below. Start by entering a project name.

Input project name window of Ktor Project Generator

2- Adjust Project Settings: For more customized setup, click “Adjust project settings.” Here, you can tweak various options like the build system, domain, and Ktor version. For the purposes of this tutorial, default settings will suffice.

Adjust project settings window of Ktor Project Generator

3- Add Plugins: Click on “Add plugins” to select any additional features for your project. For now, let’s keep it simple and select only the “Routing” plugin, essential for handling web routes in our project.

Adding plugins window of Ktor Project Generator (choosen “Routing.kt”)

4- Generate the Project: After setting up your preferences, click “Generate project” to download the project files as a zip archive.

Emphasized the “Generate project” button of Ktor Project Generator

5- Save the Instructions: The following screen will provide instructions on how to run your newly created Ktor project. It’s a good idea to save this page as an MHTML file for easy reference.

Screenshot of instructions on how to run your newly created Ktor project

With your Ktor project files ready, we’ll move on to opening and running the project in the next steps.

🚀 Opening and Starting a Ktor Project

Now that you’ve created your Ktor project, let’s get it up and running in Fleet.

1- Prepare the Project: Unzip the Ktor project file and place it in an accessible location, like your Desktop or Documents folder.

2- Open the Project in Fleet: Launch Fleet, select “Open File or Folder,” and navigate to your Ktor project folder.

Screenshot of Fleet that shows choosing opening folder.

3- Enable Smart Mode: Activate Fleet’s smart mode by clicking the ⚡(lightning) button. This process might take a few moments.

Screenshot of Fleet that shows how to enable Smart Mode

4- Resolve Any Issues: Open the Routing.kt file within the plugins directory. If you notice any red lines indicating errors, resolve them by pressing alt(option) + Enter or manually adding the necessary import statements.

5- Run the Application: Navigate to Application.kt and initiate the project by clicking the green ⯈(triangle) button.

6- Access Your App: Once the application is running, click the displayed URL (http://0.0.0.0:8080) to open your default browser and view the app.

7- View the Result: Your browser will display the “Hello World!” message.

Congratulations! You’ve successfully launched your Ktor project. If you’re feeling adventurous, try modifying the displayed text.

1- Edit the Message: Return to Routing.kt and change "Hello World!" to "Hello Ktor!".

2- Restart and Refresh: After restarting the project and refreshing the browser page, you should see “Hello Ktor!” displayed.

While simple text display could be achieved with a basic HTML file, Ktor’s true potential shines when you start adding complex back-end functionalities like user authentication or database integration.

Dive into the world of web development with Kotlin and Ktor, expanding your coding horizons beyond Android apps. 😄

<aside>
<p>
Thank you for reading!<br>
If you enjoyed this post, <br>
I'd appreciate a clap. 😄
</p>
</aside>

--

--