Creating a server
To run a Ktor server application, you need to create a server first. Server configuration can include different settings: a server engine (such as Netty, Jetty, etc.), various engine-specific options, host and port values, and so on. There are two main approaches in Ktor for creating and running a server:
The embeddedServer function is a simple way to configure server parameters in code and quickly run an application.
EngineMain provides more flexibility to configure a server. You can specify server parameters in an application.conf file and change a configuration without recompiling your application. Moreover, you can run your application from a command line and override the required server parameters by passing corresponding command-line arguments.
embeddedServer function is a simple way to configure server parameters in code and quickly run an application. In the code snippet below, it accepts an engine and port as parameters to start a server. In the example below, we run a server with the
Netty engine and listen on the
You can find the full example here: embedded-server.
EngineMain starts a server with the selected engine and loads the application module specified in the external application.conf file. Besides modules to load, this file can include various server parameters (the
8080 port in the example below).
You can find the full example here: engine-main.