Building a static file server

If you need to create a very basic static file server, that is easy to do, as well. The static file server we will create will serve the contents of the file specified in the URL path, relative to where the server was started from the Terminal. This is very similar to starting a basic HTTP file server in Python using the following command:

python -m SimpleHTTPServer 8080 

To get started with building a basic file server, we will need to follow these steps in our existing Basic Server package:

  1. First, import Foundation into main.swift by adding it to the top of the file. We will need this to use the NSData type provided by Foundation, to read the contents of a file in raw data format:
import Foundation
  1. Next, ...

Get Hands-On Full-Stack Development with Swift 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.