Start learning free

LESSON 13 OF 188 · BACKEND: YOUR FIRST API

Your First Route

Practice

A web server is a program that waits for requests and answers them. A browser, or any other program, sends a request: a method, like GET ("give me"), and a path, like / or /products. The server sends back a response: a status code that says how it went, and a body.

In Flask, the line app = Flask(__name__) creates your app, and a route teaches it to answer a path. A route is a decorator, like @app.get("/hello"), on the line right above a function:

@app.get("/hello")
def hello():
    return "Hi there!"

When a GET request for /hello arrives, Flask calls hello and sends back whatever it returns, with the status 200, which means OK. A path that no route matches gets 404 Not Found, with Flask's own HTML error page.

Your app must answer:

GET /          →  200  Welcome to the shop!
GET /products  →  404  (an HTML error page)

Your task: the app is already created for you. Add a route for the path / with a function named home that returns the text "Welcome to the shop!".


Assignment
Follow the Coding Tutorial to write the shop's first route, GET /, and its function home.


Hint
The decorator goes on the line right above def, with the path as a string in its parentheses. The return line is indented under def.


STEP 1 OF 3
    Ctrl + Enter to check