How to show different path-based content in Racket web servlets?

I am trying to follow the Racket tutorial guide on simple web applications, but cannot get one, basic, basic thing.

How can you servlet serve different materials based on the request URL? Despite my cleaning, even a huge blog example was one big file, and everything that was processed with a huge number of requests was behind me. How can I do something based on urls? The Clojure Noir framework puts this core function on the main page ( defpage ), but how to do it with Racket?

+4
source share
1 answer

The URL is part of the request structure that the servlet receives as an argument. You can get the url by calling request-uri , then you can look at it to do whatever you want. The request also includes an HTTP method, headers, etc.

But it is rather low level. The best solution is to use dispatch-rules to determine the mapping of URL patterns to handler functions. Here is an example from the docs:

 (define-values (blog-dispatch blog-url) (dispatch-rules [("") list-posts] [("posts" (string-arg)) review-post] [("archive" (integer-arg) (integer-arg)) review-archive] [else list-posts])) 

Make the main blog-dispatch servlet handler. The URL http://yoursite.com/ will be processed by a call (list-posts req) , where req is the request structure. The URL http://yoursite.com/posts/a-funny-story will be processed by a call (review-post req "a-funny-story") . And so on.

+3
source

Source: https://habr.com/ru/post/1495027/


All Articles