[racket] web server: leading forward slash required in paths?

From: Matthew Butterick (mb at mbtype.com)
Date: Tue Jan 28 17:45:27 EST 2014

The behavior is correct. It's just not the behavior you want.

When you start up the web server, the default URL for the page is:

http://localhost:8000/servlets/standalone.rkt

So "/puppy.jpg" really means this:

http://localhost:8000/puppy.jpg

Which works, because puppy.jpg is in server-root-path. Whereas the relative
URL "puppy.jpg" means this:

http://localhost:8000/servlets/puppy.jpg

And indeed, you get no puppy, because the server is looking in the
subdirectory "servlets", which doesn't exist in your server-root-path.

Thus you have two choices:

1) Add the #:servlet-path parameter, which lets you attach the servlet to a
URL at the top level, like so:

(serve/servlet
 start
 #:server-root-path SOMEPATH
 #:servlet-path "/standalone.rkt"
 )

Now when you start the web server, the URL for the page will be:

http://localhost:8000/standalone.rkt

And when you link to "puppy.jpg" (no forward slash), it will mean:

http://localhost:8000/puppy.jpg

And you will get a puppy. (Of course, "/puppy.jpg" will still work.)

2) Keep your existing server configuration and instead put a "servlets"
subdirectory within D:/Tobias/server-root/htdocs/, and puppy.jpg within it.
Then you can use "puppy.jpg" as a relative URL.

The docs for serve/servlet cover servlet-path [1], though your question is
fair because this consequence is a little unexpected. I don't know the
policy reason for having the default servlet-path
be "/servlets/standalone.rkt" rather than something at the top level, like
"/standalone.rkt".

[1] http://docs.racket-lang.org/web-server/run.html?q=serve/servlet



On Tue, Jan 28, 2014 at 1:38 PM, Janos Tobias Locsei <jtlocsei at cantab.net>wrote:

> Hi Matthew,
>
> Here's an example without using templates, but the same sort of behavior
> applies when using templates too.
>
> #lang racket
>
> (require web-server/servlet
>          web-server/servlet-env)
>
> (define SOMEPATH
>   (build-path "D:\\Tobias\\server-root"))
>
> (define (start request)
>   (response/xexpr
>    `(html (head (title "Hello world"))
>           (body (h1 "Hello world")
>                 (img ([src "/puppy.jpg"] [alt "cute puppy"]))))))
> ; puppy.jpg is located at D:/Tobias/server-root/htdocs/puppy.jpg
> ; Image only displays if I use the leading forward slash like this
>
> (serve/servlet
>  start
>  #:server-root-path SOMEPATH
>  )
>
> I noticed that the leading forward slash is used in the web applications
> tutorial of the racket documentation, e.g. when referencing a css file
> (search page for "test-static.css"):
> http://docs.racket-lang.org/continue/
>
> Tobias
>
>
>
>
> On 28 January 2014 19:27, Matthew Butterick <mb at mbtype.com> wrote:
>
>> I've not experienced this issue with the Racket web server. When you
>> append a leading forward slash to a URL, you're obviously telling the
>> browser something completely different about where to find the file. I
>> would suspect it has something to do with either file organization, Racket
>> web server configuration, or both.
>>
>> Where are myimage.jpg and mytemplate.html in relation to one another? In
>> the same directory?
>>
>>
>> On Tue, Jan 28, 2014 at 7:04 AM, Janos Tobias Locsei <jtlocsei at cantab.net
>> > wrote:
>>
>>> I noticed that when using the racket web server one needs to include a
>>> leading forward slash when referencing static files e.g. a style.css or an
>>> image.jpg. Is there a rationale for this? It's different than the usual
>>> html convention, and it means that when using html template files it's not
>>> possible to preview the template in the web browser without running the web
>>> server.
>>>
>>> For example, I have a file mytemplate.html that includes the line
>>>
>>> <img src="/myimage.jpg" />
>>>
>>> The leading "/" is required for the image to be found by the racket web
>>> server but it means that I can't preview mytemplate.html in my web browser
>>> without first firing up the racket web server.
>>>
>>> It's not a big deal but I'm just wondering if there's a way around this.
>>>
>>> Tobias
>>>
>>>
>>>
>>>
>>>
>>> ____________________
>>>   Racket Users list:
>>>   http://lists.racket-lang.org/users
>>>
>>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140128/74b485ab/attachment.html>

Posted on the users mailing list.