Getting Started with Vapor 3
Vapor is the leading server side Swift framework in the iOS community and it enables Swift developers to take their existing Swift skills to the cloud. After the success of Vapor 1 and Vapor 2, Vapor team made it even better by creating the Vapor 3 framework. At the time of this writing, Vapor 3 is still not released but stable enough to be used in applications.
In this post I will cover how to get started with Vapor 3.
Installing Vapor 3
Vapor documentation provides detailed instructions on how to install Vapor 3 framework. Instead of repeating the installation process, I will simply link to the process here.
Hello Vapor
After installing Vapor, next step is to initialize Vapor project. This can be done in the terminal by running the following command.
vapor new hello-vapor
This will create a new “hello-vapor” project folder which contains all default files to get started with Vapor. Jump into the hello-vapor folder and run the following command.
vapor xcode
This command is going to create a Xcode project for your Vapor project. Once the process is completed, you will end up with an xcodeproj file for your hello-vapor project. Now, you can open your Vapor project in Xcode by simply double clicking on the Xcode project file.
Before building your app make sure the target is set to Run and My Mac is selected as the device as shown in the following screenshot:
After the build is complete, run your app. This will start the server on port 8080. Visit the http://localhost:8080/hello in your browser to see the hello route being rendered.
Congratulations! You are now running Swift on the server using Vapor framework.
Routing
By default Vapor adds some boiler level code in the routes.swift file which creates the default routes for your Vapor project. You can create your own routes in routes.swift file. Let’s add a route for movies as shown below:
The router object is of type Router which plays an integral role in routing the requests to the correct path. We used the get function on the router object and specified a path called “movies”. When we go to the movies a closure is invoked which has access to the request object. The closure returns a string value “movies” to the user.
To try this out, run the project and visit the http://localhost:8080/movies url. The screenshot below shows the result.
You can even nest different routes together as shown below:
Line number 14,15,16 will produce the same result as 10,11,12. It is just a different way of writing the same thing. The result is shown below:
Instead of creating a route for each possible genre, you can use the power of Route Parameters as shown below:
The result is shown below:
Thanks to Vapor Parameters we don’t have to create separate routes for each genre.
Vapor also provides easy way to access Query Strings, Grouping Routes and much much more etc. I discuss that in great detail in my course “Mastering Server Side Swift Using Vapor 3”. You can use the coupon code below to get a discount on the course. I hope you enjoyed this article! Happy coding!