Using React in DataKernel projects

Introduction

In this example you can explore how to integrate React in DataKernel projects. You can find full example sources on GitHub.

Here we will consider DataKernel HttpServerLauncher and AsyncServlet which are used to set up the server, which processes the requests. See how DataKernel makes this process extremely simple.

Creating launcher

SimpleApplicationLauncher extends DataKernel HttpServerLauncher:

public final class SimpleApplicationLauncher extends HttpServerLauncher {
	@Provides
	Executor executor() {
		return newSingleThreadExecutor();
	}

	@Provides
	AsyncServlet servlet(Executor executor) {
		return StaticServlet.ofClassPath(executor, "build")
				.withIndexHtml();
	}

	public static void main(String[] args) throws Exception {
		SimpleApplicationLauncher launcher = new SimpleApplicationLauncher();
		launcher.launch(args);
	}
}

HttpServerLauncher takes care of setting up all the needed configurations for HTTP server.

Provide AsyncServlet, which will open the index.html of the provided path.

Then write down main method, which will launch SimpleApplicationLauncher. And that’s it, no additional configurations are required.

Running the application

If you want to run the example, clone DataKernel and import it as a Maven project. Before running the example, build the project (Ctrl + F9 for IntelliJ IDEA).

Run the following command in example’s folder in terminal:

$ npm run-script build

Open SimpleApplicationLauncher class and run its main() method. Then open your favourite browser and go to localhost:8080.