Posted 7/5/2018.
Vapor is a web framework for Swift. It runs on macOS, iOS, and Linux, and can be used to build websites, web applications, and APIs. Frameworks like Vapor make it possible to build entire products in Swift, including a backend, web app, marketing site, and Apple device app (iOS, macOS, tvOS, watchOS). In fact, this website was build using Vapor.
In this series we'll build a basic API for managing information about U.S. national parks. We'll start in this tutorial by installing Vapor, setting up a Vapor project, and running a Vapor app in Xcode.
This tutorial is current as of Xcode 9.4.1.
eval "$(curl -sL check.vapor.sh)"
✅ Xcode 9 is compatible with Vapor 2.
✅ Xcode 9 is compatible with Vapor 3.
✅ Swift 4.1 is compatible with Vapor 2.
✅ Swift 4.1 is compatible with Vapor 3.
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Then, run:
brew install vapor/tap/vapor
Run vapor --help
in Terminal. You should see a list of commands you can run with vapor
. We'll be covering most of them in this series.
Run vapor new --help
in Terminal to see more information on what you can do with the new
command. Try this with a few other commands listed in 4).
Navigate to the directory you would like to use for your Vapor project.
cd ~/path/to/directory
vapor new NationalParks
vapor xcode -y --verbose
When running
vapor xcode
, Vapor will ask you if you want to open the new Xcode project when it finishes. Running the command with '-y' tells Vapor you want to open it automatically. Adding '-- verbose' shows you what Vapor is doing as it generates the project.
vapor --version
You should see the following (or a later version):
Vapor Toolbox: 3.1.7
Vapor Framework: 3.0.4
The above setup generates an Xcode project using Vapor's default template, api
. This template includes an example model (Todo
) and controller along with some sample routes. To see the default routes, go to routes.swift
. You should see the following code at the top:
router.get("hello") { req in
return "Hello, world!"
}
This is one of the most basic routes you can write in Vapor, a simple GET request that prints "Hello, world!".
To test out the new app, in Xcode switch to the Run
scheme, build, and run. Once you see the log message in Xcode, go to localhost:8080/hello
in the browser of your choice. You should see "Hello, world!".
In this tutorial we saw how to install Vapor and build the default api
template. This template includes a lot of code you may not need. Vapor is a powerful framework with a lot of flexibility and a growing community of open source contributors, with a range of packages for building modern APIs. In the next part we'll cover the structure of a Vapor app.