Sample application for Kitura Web Framework
Summary
This Kitura sample shows off the powerful features available in Kitura 2, baking several demos into one project. You can access the individual examples by navigating to their specific routes in a browser.
It features the following:
- Kitura WebSocket based chat server
- Stencil template engine example
- Using multiple handlers per route
- Reading and accepting parameters in a route
Getting Set Up
-
Install the prerequisites (ignore the requirement to install Homebrew on macOS as it is not required for this sample).
-
git clone https://github.com/IBM-Swift/Kitura-Sample.git && cd Kitura-Sample
Note: do not use the GitHub "Download ZIP" button
-
swift build
-
./.build/debug/Kitura-Sample
You should see a message Listening on port 8080. You may need to click "Allow" if a security pop up appears, dependent on your firewall settings.
-
Open your browser at http://localhost:8080.
-
Navigate to one of the example routes on the localhost URL, for example, for the chat server go to localhost:8080/chat.
Available Examples
Kitura WebSocket
Route: localhost:8080/chat
This demo sets up a local chat server using Kitura's WebSocket library, and the UI mimicks a chat room. Two separate browser windows pointed to the /chat
route can be used to represent two people in the chat room if the project is running on localhost. It can also be deployed to the IBM Cloud and then accessed via a Cloud Foundry App.
Stencil Templating Engine
Route: localhost:8080/articles
Kitura supports the popular Stencil Templating Engine, using the Kitura Stencil library. This route looks for a Stencil file in a subdirectory called Views
and renders the page with it.
Multiple Handlers in one Route
Route: localhost:8080/multi
Kitura supports multiple handlers per route, this example prints several lines to the window using different response.send()
methods.
Reading and Accepting Parameters
Route: localhost:8080/users/:user
Note: This example uses :users
but you can use anything you like, as long as the first part of the route is /users/
.
This route accepts a parameter in its URL and uses that parameter in its HTML creation. It does this using a colon (:) in the URL which defines the item following it as a parameter. It then assigns this parameter to a variable, and concatenates it into the HTML.
Testing
To run the tests locally, run swift test
from the Kitura-Sample directory.
Running in Xcode
You can also run this sample application inside Xcode. For more details, visit kitura.io.
License
This sample app is licensed under the Apache License, Version 2.0.