Start Using Vapor 4 (with Swift)
So today I want to share my experience when make Server-Side Project using Vapor with Swift. We will start with a very simple project (“Hello World Projectâ€). This is the first of many articles I will write to discuss using Vapor with Swift. Okay, LET’S START IT!!! YEAY…
Intalling on MacOS
- Open Terminal
- We will install the Vapor using HomeBrew. If you don’t have Homebrew, you can download it from https://brew.sh/
brew install vapor/tap/vapor
Make The Project
Still using the Terminal, we will add a new Folder for our project:
mkdir MyFirstProject cd MyFirstProject
Then let’s use the Vapor’s template for new project
vapor new MyFirstProject
Then there will be a question Would you like to use Fluent? , for now we don’t need it so we can write n there.
You will see like below here:
It means the progress is finish.
Now you can build it and run it.
cd MyFirstProject vapor build vapor run
Then you can try open your browser then open localhost:8080
You can open the Folder with Finder using open . command on your terminal.
You can see that it is very different with project that you make with XCode. There’s no xcodeproj file there. You still can open your project with XCode, you can use vapor xcode -y to open it in XCode, or just open any Swift file there.
NOTE: Don’t create the xcodeproj using swift package generate-xcodeproj because the project won’t run properly.
If we see in Project Navigator on XCode, there’s a file called Package.swift and there’s a section called Swift Package Dependencies. We use Swift Package Manager or SwiftPM (it’s similar to Cocoapods in iOS, npm in Node.JS). We use it to download and import every package that we need for our project (vapor is one of the package that imported by SwiftPM)
Okay, we will continue again in the next article. I hope this article can help you start learning about Swift Server-Side using Vapor. Thank you. ??