What is HTTP routing?

Source: Internet
Author: User
Recently, when I was looking at some frameworks, there were some routes that I didn't quite understand. What is the difference between them and the routes in the network? recently, when I was looking at some frameworks, there were routes that I didn't quite understand, what is the difference between this and routing in the network?

Reply content:

I recently read some frameworks, but I don't quite understand them. What is the difference between this and routing in the network?

A route can be said to be a program that processes the relationship between url and function. configure a series of url access rules to provide templates for url access. For example, the python flask framework has the following code:

@app.route('/user')def index():      return 'Hello'      @app.route('/')def index():      return 'Home'

In this way, you are accessingYour domain name/userThe requested html template isHello, AccessYour domain name/, Which appearsHome

It is the ing between the url and the function or class.

For the framework, it is the process of directing a request path to your target class and method.
As for how to analyze it, there are different implementations and highlights.
It is essentially similar to the network-level routes, but they actually have different levels of things. They have different levels of life.

The routing function is path ing. There are two common URL modes.
www.x.com/a/b/c/dOrwww.x.com/index.php?a=x&b=y
The routing function maps an address to another address, for examplewww.x.com/x/y.html<=>www.x.com/index.php?a=x&b=y
When you accesswww.x.com/x/y.htmlThe access iswww.x.com/index.php?a=x&b=yIn addition to common/Other symbols can also be used to represent the path, for example:www.x.com/x_y.html,www.x.com/xy.html

Furthermore,index.php?a=x&b=yURL exposeda,bFor the two parameters, use/x/y.htmlThe form is hidden to play a certain security role.

The essence of the routing in the framework is to convert the URL into a class path.

For examplehttps://segmentfault.com/user/note

The routing function isuserClass,note.

Finally, let the framework know that you want to execute the note method of the user class.

There are many identification methods. Different frameworks and url formats have different matching rules.

Sf is relatively simple and can be matched with regular expressions (or even/You can also make a simple match ).

···········
From the url, use the following rules:/Split the url. 0th is the domain name, and 1st and 2 are the class names and methods respectively.
Because there is no module name, add the default module 'home'
The class path to be accessed is: Project and directory/module path (linked to the project structure)/Home module/Controller Layer/User Controller
Then, the related classes are automatically loaded, the user controller is instantiated, and the note method is executed.
···········

I used sf as an example. In fact, the framework must have processed some information, which is not that simple.

The routing function is to convert the URL into a class path.

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.