Chapter 5. Writing a To-Do List with Kitura
This chapter explains how to build a simple web service in Swift. The service stores a list of to-do list items in a database, and is accessed with a REST
interface.
Along the way, you will learn to:
- Create handlers for
GET
andPOST
requests - Receive and send JSON
- Deploy your application to Bluemix
- Do error handling
- Use authentication in your application
You’ll use Kitura, which is a Swift web framework library produced at IBM that helps developers build web applications. This framework includes an HTTP server that can listen for new client connections and route their requests to an appropriate handler. Its treatment of URL routing and pluggable middleware follows the course set by other popular web middleware frameworks like Sinatra (from Ruby), Flask (from Python), and Express.js (from NodeJS).
Servers and Routers
In order to direct incoming requests to places in your program, you must register routes with an instance of Kitura’s Router
class. After you import the Kitura module with SwiftPM, your code must create the router:
let
router
=
Router
()
Before an incoming request can get to the router, an HTTP server must be listening on a TCP port. Create the server, register it to your Router
, and have it listen on port 8090:
Kitura
.
addHTTPServer
(
onPort
:
8090
,
with
:
router
)
Kitura
.
run
()
Multiple Servers
The Kitura runtime supports creating multiple HTTP servers. These servers can be listening on different ports, and you can also ...
Get Extending Swift Value(s) to the Server now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.