{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"required","examples":{"codes":[]},"params":[],"results":{"codes":[]},"settings":""},"next":{"description":"","pages":[]},"title":"Hello, World","type":"basic","slug":"hello-world","excerpt":"","body":"This section assumes you have installed Swift 3 and the Vapor CLI and have verified they are working.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"New Project\"\n}\n[/block]\nLet's start by creating a new project called Hello, World\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"vapor new Hello\",\n      \"language\": \"shell\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Folder Structure\"\n}\n[/block]\nVapor's folder structure will probably look familiar to you if you have worked with other web frameworks.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \".\\n├── App\\n│   └── Controllers\\n│   └── Middleware\\n│   └── Models\\n│   └── main.swift\\n├── Public\\n├── Resources\\n│   └── Views\\n└── Package.swift\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\nFor our Hello, World project, we will be focusing on the `main.swift` file.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \".\\n└── App\\n    └── main.swift\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\nOpen `main.swift` in your favorite text editor.\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Example Project\",\n  \"body\": \"The `vapor new` command creates a new project with examples and comments about how to use the framework. You can delete these if you want.\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Droplet\"\n}\n[/block]\nLook for the following line in the `main.swift` file.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"let drop = Droplet()\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nThis is where the one and only `Droplet ` for this example will be created. The `Droplet ` class has a plethora of useful functions on it, and is used extensively.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Routing\"\n}\n[/block]\nRight after the creation of `drop`, add the following code snippet.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"drop.get(\\\"hello\\\") { request in\\n    return \\\"Hello, world\\\"\\n}\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nThis creates a new route on the `Droplet` that will match all `GET` requests to `/hello`. All route closures are passed an instance of `Request` that contains information such as the URI requested and data sent. We will learn more about requests later.This route simply returns a string. We will learn more about what can be returned by route closures later. \n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Starting\"\n}\n[/block]\nAt the bottom of the main file, make sure to start your application.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"drop.serve()\",\n      \"language\": \"swift\"\n    }\n  ]\n}\n[/block]\nSave the file, and switch back to the terminal.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Compiling\"\n}\n[/block]\nA big part of what makes Vapor so great is Swift's state of the art compiler. Let's fire it up. Make sure you are in the root directory of the project and run the following command.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"vapor build\",\n      \"language\": \"shell\"\n    }\n  ]\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Toolbox Shortcut\",\n  \"body\": \"`vapor build` runs `swift build` in the background.\"\n}\n[/block]\nThe Swift Package Manager will first start by downloading the appropriate dependencies from git. It will then compile and link these dependencies together.When the process has completed, you will see output similar to `Linking .build/debug/App`\n[block:callout]\n{\n  \"type\": \"warning\",\n  \"title\": \"Process Killed\",\n  \"body\": \"If you see a message like `unable to execute command: Killed`, you need to increase your swap space. This can happen if you are running on a machine with limited RAM.\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Run\"\n}\n[/block]\nBoot up the server by running the following command.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"vapor run serve\\n\\n# Optionally specify port\\nvapor run serve --port=8080\",\n      \"language\": \"shell\"\n    }\n  ]\n}\n[/block]\nYou should see a message `Server starting...`. You can now visit `http://localhost/hello` in your browser.\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Permission Denied\",\n  \"body\": \"Certain port numbers require super user access to bind. Simply run `sudo vapor run` to allow access. If you decide to run on a port besides 80, make sure to direct your browser accordingly.\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Hello, World\"\n}\n[/block]\nYou should see the following output in your browser window.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Hello, world\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]","updates":["570c38992bf01d2000c059a3","577e9506781f3e0e005fbbd9"],"order":4,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"5768701b72cd4c0e000fad2d","user":"57086f3d447c410e00221bd1","version":{"version":"0.12","version_clean":"0.12.0","codename":"Client Clyde","is_stable":false,"is_beta":true,"is_hidden":false,"is_deprecated":true,"categories":["5768701b72cd4c0e000fad19","5768701b72cd4c0e000fad1a","5768701b72cd4c0e000fad1b","5768701b72cd4c0e000fad1c"],"_id":"5768701b72cd4c0e000fad18","releaseDate":"2016-06-20T22:37:15.641Z","__v":1,"createdAt":"2016-06-20T22:37:15.641Z","project":"57086fe855a4690e005de407"},"githubsync":"","__v":12,"category":{"sync":{"isSync":false,"url":""},"pages":[],"title":"Getting Started","slug":"documentation","order":0,"from_sync":false,"reference":false,"_id":"5768701b72cd4c0e000fad19","createdAt":"2016-04-09T02:58:48.377Z","__v":0,"project":"57086fe855a4690e005de407","version":"5768701b72cd4c0e000fad18"},"project":"57086fe855a4690e005de407","createdAt":"2016-04-09T06:21:14.866Z","parentDoc":null}
This section assumes you have installed Swift 3 and the Vapor CLI and have verified they are working. [block:api-header] { "type": "basic", "title": "New Project" } [/block] Let's start by creating a new project called Hello, World [block:code] { "codes": [ { "code": "vapor new Hello", "language": "shell" } ] } [/block] [block:api-header] { "type": "basic", "title": "Folder Structure" } [/block] Vapor's folder structure will probably look familiar to you if you have worked with other web frameworks. [block:code] { "codes": [ { "code": ".\n├── App\n│ └── Controllers\n│ └── Middleware\n│ └── Models\n│ └── main.swift\n├── Public\n├── Resources\n│ └── Views\n└── Package.swift", "language": "text" } ] } [/block] For our Hello, World project, we will be focusing on the `main.swift` file. [block:code] { "codes": [ { "code": ".\n└── App\n └── main.swift", "language": "text" } ] } [/block] Open `main.swift` in your favorite text editor. [block:callout] { "type": "info", "title": "Example Project", "body": "The `vapor new` command creates a new project with examples and comments about how to use the framework. You can delete these if you want." } [/block] [block:api-header] { "type": "basic", "title": "Droplet" } [/block] Look for the following line in the `main.swift` file. [block:code] { "codes": [ { "code": "let drop = Droplet()", "language": "swift" } ] } [/block] This is where the one and only `Droplet ` for this example will be created. The `Droplet ` class has a plethora of useful functions on it, and is used extensively. [block:api-header] { "type": "basic", "title": "Routing" } [/block] Right after the creation of `drop`, add the following code snippet. [block:code] { "codes": [ { "code": "drop.get(\"hello\") { request in\n return \"Hello, world\"\n}", "language": "swift" } ] } [/block] This creates a new route on the `Droplet` that will match all `GET` requests to `/hello`. All route closures are passed an instance of `Request` that contains information such as the URI requested and data sent. We will learn more about requests later.This route simply returns a string. We will learn more about what can be returned by route closures later. [block:api-header] { "type": "basic", "title": "Starting" } [/block] At the bottom of the main file, make sure to start your application. [block:code] { "codes": [ { "code": "drop.serve()", "language": "swift" } ] } [/block] Save the file, and switch back to the terminal. [block:api-header] { "type": "basic", "title": "Compiling" } [/block] A big part of what makes Vapor so great is Swift's state of the art compiler. Let's fire it up. Make sure you are in the root directory of the project and run the following command. [block:code] { "codes": [ { "code": "vapor build", "language": "shell" } ] } [/block] [block:callout] { "type": "info", "title": "Toolbox Shortcut", "body": "`vapor build` runs `swift build` in the background." } [/block] The Swift Package Manager will first start by downloading the appropriate dependencies from git. It will then compile and link these dependencies together.When the process has completed, you will see output similar to `Linking .build/debug/App` [block:callout] { "type": "warning", "title": "Process Killed", "body": "If you see a message like `unable to execute command: Killed`, you need to increase your swap space. This can happen if you are running on a machine with limited RAM." } [/block] [block:api-header] { "type": "basic", "title": "Run" } [/block] Boot up the server by running the following command. [block:code] { "codes": [ { "code": "vapor run serve\n\n# Optionally specify port\nvapor run serve --port=8080", "language": "shell" } ] } [/block] You should see a message `Server starting...`. You can now visit `http://localhost/hello` in your browser. [block:callout] { "type": "info", "title": "Permission Denied", "body": "Certain port numbers require super user access to bind. Simply run `sudo vapor run` to allow access. If you decide to run on a port besides 80, make sure to direct your browser accordingly." } [/block] [block:api-header] { "type": "basic", "title": "Hello, World" } [/block] You should see the following output in your browser window. [block:code] { "codes": [ { "code": "Hello, world", "language": "text" } ] } [/block]