Swift3.0 Server Development (ii) static file additions, routing configuration, and form submission

Source: Internet
Author: User
Tags form post java web

Let's talk about it today. Perfect Framework's static file additions and accesses , the configuration of routes, and the submission of forms . While the official web site has a section on accessing static files, there are some points to note when using the perfect framework to access static files, which are not presented on their official documents. What we are going to do today is to access the static file through the browser, and then use the form form in the static file to submit the corresponding data on the specified route.

I. Adding and accessing static files

1. Configuration of the static file root directory for perfect that are not managed with Xcode

In PHP development or Java Web Development, there is a root directory to store the corresponding static files, such as wwwroot, Htdoc, Webroot , and so on. This is also true when using perfect to develop the server side, we can configure the root directory of the static file in the Main.swift in the source directory, below is our configuration code:

  

After the configuration is complete, if our project is not managed with Xcode , when we compile and run the perfect project, the root directory of the corresponding static file (Webroot) is created in the appropriate template directory. As shown below:

  

In Webroot we add the corresponding static file, we can access through the Perfect service, below is the content of index.html we added in webroot , It can be accessed with a browser. Below is our relatively simple index.html content, of course, below the HTML simple can not be simple, as follows:

  

Before accessing the above-mentioned index.html file, we need to delete the route added in Main.swift , and the selected section below is the code to be removed. If you do not delete the code below, if you visit localhost:8181 , then it is the content returned by the route below. After modifying the code, it is not effective to re-compile the run, which is different from the explanatory language PHP.

  

After the above steps, we can load our index.html file by accessing localhost:8181, as shown below:

  

2. Perfect static file directory under Xcode management

The above steps are in cases where you are not using Xcode to manage the perfect project, and the Webroot folder is located in the project directory . When our perfect project is managed using Xcode, the location of the Webroot will be changed . Below is the package manager that calls Swift to generate the corresponding xcodeproj file:

  

If you have a webroot directory in your project directory, there are also index.html files. However, after using Xcode for Perfect project Management, you cannot find the index.html file below. That is, webroot 's file directory changed place. This is not covered in the official documentation.

    

By searching, we can find the webroot directory in the perfect project under Xcode management, use Xcode to open the perfect project, find product, then right-click, Show in Finder.

  

We will see the Webroot directory under Xcode management as shown below. You can place static files in wwwroot to access them.

  

When we refresh our browser here, we can find the corresponding index.html.

  

Second, the routing configuration

The code snippet below is Main.swift , first creating an object of httpserver () , then creating a Routing object, and then adding the routing object to the service object. The specific code looks like this:

  

1. Add a static route path

We can add various paths to the route by calling the Add () method of the routes object, although add () has many overloaded methods, but its function is to add a path to the route, request a method, or create a handle.

  

Below we have added a routing path for "/login" , which is requested by the . Get way. The trailing closure of the rear is the processing of the response. The specific code is as follows:

  

Recompiling and running our perfect project and then accessing the localhost:8181/login path will see the return information below in the browser. The other routes

  

2. Routing variables

When configuring routing, we can add route variables to match URLs of the same type in our path. Below is the use of the Perfect frame in the middle of the variable. The route variable is inserted in the URL by means of {key} . The middle {\ (Valuekey)} in the lower URI is the key of the route variable that is inserted through the string interpolation, and we can get the corresponding route variable by this key.

  

Below is the result of our access to the above path:

  

3. Routing wildcard characters

We can also use wildcard class matching paths in URIs , using * to match common paths. Below is how a route wildcard is used:

  

Below is the route wildcard is the result of the run:

  

4. End Wildcard

Below is how the end wildcard is used, with "* *" at the end to match all of the trailing URI, and then Routetrailingwildcardkey to get the contents of the wildcard.

  

The access results are as follows:

  

The priority of these four routes is: route variable > static route > wildcard path > end wildcard character . The official documentation also has an example of configuring the version of the routing table, which is not too much to repeat here.

Third, form submission and JSON return

After talking about the configuration of the route, let's talk about the form's submission and the return of the JSON data. In this section, we first use the Form form post submission to submit the data to the specified route and process it, and then the processed data is returned to the client through the JOSN data format.

1. Building Form Form

First we have to create the corresponding form submission code, of course, the code is relatively simple, just a from form. The address of the action is the routing address "127.0.0.1:8181/login" that we configured on the server side, and the form is submitted in the same way as POST. The details are as follows.

  

2. Get Form form Parameters

Below is the server-side code that gets the form parameters. First we add a "/login" route, and the route is requested in . Post. First we get the parameters of the request. Because the request parameter names for form forms are "userName" and "password", we use these two name from request object to get the corresponding value. After getting the value you can query the database or some other operation. The appropriate JSON information is returned when the operation is complete. The dictionary in Swift can be converted by calling the jsonencodestring () method, and the back blog will parse the source code of some Perfect frameworks, which will involve translating the dictionary into JSON string , here do not do too much to repeat.

  

3. Form submission and result return

Both the Web page and the server end have been implemented, and then we are ready to test. Below is the corresponding form form with the result returned:

   

This blog will be here first, the next blog will still be the perfect framework of the relevant content to introduce.

Swift3.0 Server Development (ii) static file additions, routing configuration, and form submission

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.