Web application static files

Sunday, 1 May 2016 · 7 messages · last reply 6 May 2016
MG
Murray, Gregory1 May 2016 18:43 #
Hello, I'm new to BaseX, and I'm setting up a simple web application (on Mac/Unix, using the ZIP distribution of BaseX). I'm unable to bring in static files like CSS. Following the documentation and the "dba" web application that comes with BaseX, I have included the following in the HTML:
<link rel="stylesheet" type="text/css" href="static/app.css"/>
to point to a file that resides here: ~/basex/webapp/bookplates/static/app.css I have a module sitting here: ~/basex/webapp/bookplates/controller.xqm with a function with these annotations: %rest:path("bookplates") %output:method("html") When I point my browser to http://localhost:8984/bookplates/ the server successfully returns the HTML, but when the browser tries to load the CSS it requests http://localhost:8984/bookplates/static/app.css and the server responds with a 404 saying "No function found that matches the request" as if the server is looking for a RESTXQ function. What am I doing wrong? Thanks, Greg
CG
Christian Grün2 May 2016 09:20 #
Hi Greg, Thanks for your mail. If you are working with the ZIP distribution, you could compare your code with the RESTXQ examples in the restxq.xqm file. I assume you get 404 because your static directory is a sub-directory of bookplates. Here are two things you can do: * Move your static directory from "webapp/bookplates" to "webapp" * Deliver static files via RESTXQ (see common.xqm and the REST path "/dba/static/{$file=.+}"). Does this help? Christian
Quoted text · Gregory · 27 lines
On Sun, May 1, 2016 at 6:43 PM, Murray, Gregory <gregory.murray at ptsem.edu> wrote: > Hello, > > I'm new to BaseX, and I'm setting up a simple web application (on Mac/Unix, using the ZIP distribution of BaseX). I'm unable to bring in static files like CSS. Following the documentation and the "dba" web application that comes with BaseX, I have included the following in the HTML: > > <link rel="stylesheet" type="text/css" href="static/app.css"/> > > to point to a file that resides here: > > ~/basex/webapp/bookplates/static/app.css > > I have a module sitting here: > > ~/basex/webapp/bookplates/controller.xqm > > with a function with these annotations: > > %rest:path("bookplates") > %output:method("html") > > When I point my browser to http://localhost:8984/bookplates/ the server successfully returns the HTML, but when the browser tries to load the CSS it requests http://localhost:8984/bookplates/static/app.css and the server responds with a 404 saying "No function found that matches the request" as if the server is looking for a RESTXQ function. > > What am I doing wrong? > > Thanks, > Greg
MG
Murray, Gregory3 May 2016 02:59 #
Hi Christian, Thanks for the hints! I see that using webapp/static does work, but I prefer to keep all files related to an app together under one directory, so I'll use the second option you provided -- using RESTXQ. Many thanks, Greg
Quoted text · Christian Grün · 43 lines
From: Christian Grün [christian.gruen at gmail.com] Sent: Monday, May 02, 2016 3:20 AM To: Murray, Gregory Cc: basex-talk at mailman.uni-konstanz.de Subject: Re: [basex-talk] Web application static files Hi Greg, Thanks for your mail. If you are working with the ZIP distribution, you could compare your code with the RESTXQ examples in the restxq.xqm file. I assume you get 404 because your static directory is a sub-directory of bookplates. Here are two things you can do: * Move your static directory from "webapp/bookplates" to "webapp" * Deliver static files via RESTXQ (see common.xqm and the REST path "/dba/static/{$file=.+}"). Does this help? Christian On Sun, May 1, 2016 at 6:43 PM, Murray, Gregory <gregory.murray at ptsem.edu> wrote: > Hello, > > I'm new to BaseX, and I'm setting up a simple web application (on Mac/Unix, using the ZIP distribution of BaseX). I'm unable to bring in static files like CSS. Following the documentation and the "dba" web application that comes with BaseX, I have included the following in the HTML: > > <link rel="stylesheet" type="text/css" href="static/app.css"/> > > to point to a file that resides here: > > ~/basex/webapp/bookplates/static/app.css > > I have a module sitting here: > > ~/basex/webapp/bookplates/controller.xqm > > with a function with these annotations: > > %rest:path("bookplates") > %output:method("html") > > When I point my browser to http://localhost:8984/bookplates/ the server successfully returns the HTML, but when the browser tries to load the CSS it requests http://localhost:8984/bookplates/static/app.css and the server responds with a 404 saying "No function found that matches the request" as if the server is looking for a RESTXQ function. > > What am I doing wrong? > > Thanks, > Greg
ML
Marco Lettere6 May 2016 13:49 #
Hi Christian, I tried the approach of serving static web resources from RestXQ. First I encountered a problem related to my js files being served to the browser as b64 encoded and the browser isn't able to interpret the script. So I modified the code in order to recognize text resources and apply file:read-text but then the < are escaped to &lt; and again the browser complains with syntax errors. What am I missing here? I'm trying with the latest snapshot, btw. This is the code I'm using
declare
   %rest:path("/dasbox/ui/resources/{$resource=.+}")
   %rest:GET
   %output:method("html")
function ui:resources($resource as xs:string){
   let $path := file:base-dir() || 'resources/' || $resource
   let $mediatype := web:content-type($path)
   return (
     web:response-header(map { 'media-type': $mediatype }),
     if (matches($mediatype, "^text/.+")) then file:read-text($path)
     else file:read-binary($path)
   )
};
Thanks for your support, Marco.
Quoted text · Gregory · 60 lines
On 03/05/2016 02:59, Murray, Gregory wrote: > Hi Christian, > > Thanks for the hints! I see that using webapp/static does work, but I prefer to keep all files related to an app together under one directory, so I'll use the second option you provided -- using RESTXQ. > > Many thanks, > Greg > > ________________________________________ > From: Christian Grün [christian.gruen at gmail.com] > Sent: Monday, May 02, 2016 3:20 AM > To: Murray, Gregory > Cc: basex-talk at mailman.uni-konstanz.de > Subject: Re: [basex-talk] Web application static files > > Hi Greg, > > Thanks for your mail. If you are working with the ZIP distribution, > you could compare your code with the RESTXQ examples in the restxq.xqm > file. > > I assume you get 404 because your static directory is a sub-directory > of bookplates. Here are two things you can do: > > * Move your static directory from "webapp/bookplates" to "webapp" > * Deliver static files via RESTXQ (see common.xqm and the REST path > "/dba/static/{$file=.+}"). > > Does this help? > Christian > > > > On Sun, May 1, 2016 at 6:43 PM, Murray, Gregory > <gregory.murray at ptsem.edu> wrote: >> Hello, >> >> I'm new to BaseX, and I'm setting up a simple web application (on Mac/Unix, using the ZIP distribution of BaseX). I'm unable to bring in static files like CSS. Following the documentation and the "dba" web application that comes with BaseX, I have included the following in the HTML: >> >> <link rel="stylesheet" type="text/css" href="static/app.css"/> >> >> to point to a file that resides here: >> >> ~/basex/webapp/bookplates/static/app.css >> >> I have a module sitting here: >> >> ~/basex/webapp/bookplates/controller.xqm >> >> with a function with these annotations: >> >> %rest:path("bookplates") >> %output:method("html") >> >> When I point my browser to http://localhost:8984/bookplates/ the server successfully returns the HTML, but when the browser tries to load the CSS it requests http://localhost:8984/bookplates/static/app.css and the server responds with a 404 saying "No function found that matches the request" as if the server is looking for a RESTXQ function. >> >> What am I doing wrong? >> >> Thanks, >> Greg
CG
Christian Grün6 May 2016 17:23 #
Hi Marco, Does the behavior change if you remove output:method("html")? Cheers, Christian
Quoted text · Marco Lettere · 92 lines
> declare > %rest:path("/dasbox/ui/resources/{$resource=.+}") > %rest:GET > %output:method("html") > function ui:resources($resource as xs:string){ > let $path := file:base-dir() || 'resources/' || $resource > let $mediatype := web:content-type($path) > return ( > web:response-header(map { 'media-type': $mediatype }), > if (matches($mediatype, "^text/.+")) then file:read-text($path) > else file:read-binary($path) > ) > }; > > Thanks for your support, > Marco. > > > On 03/05/2016 02:59, Murray, Gregory wrote: >> >> Hi Christian, >> >> Thanks for the hints! I see that using webapp/static does work, but I >> prefer to keep all files related to an app together under one directory, so >> I'll use the second option you provided -- using RESTXQ. >> >> Many thanks, >> Greg >> >> ________________________________________ >> From: Christian Grün [christian.gruen at gmail.com] >> Sent: Monday, May 02, 2016 3:20 AM >> To: Murray, Gregory >> Cc: basex-talk at mailman.uni-konstanz.de >> Subject: Re: [basex-talk] Web application static files >> >> Hi Greg, >> >> Thanks for your mail. If you are working with the ZIP distribution, >> you could compare your code with the RESTXQ examples in the restxq.xqm >> file. >> >> I assume you get 404 because your static directory is a sub-directory >> of bookplates. Here are two things you can do: >> >> * Move your static directory from "webapp/bookplates" to "webapp" >> * Deliver static files via RESTXQ (see common.xqm and the REST path >> "/dba/static/{$file=.+}"). >> >> Does this help? >> Christian >> >> >> >> On Sun, May 1, 2016 at 6:43 PM, Murray, Gregory >> <gregory.murray at ptsem.edu> wrote: >>> >>> Hello, >>> >>> I'm new to BaseX, and I'm setting up a simple web application (on >>> Mac/Unix, using the ZIP distribution of BaseX). I'm unable to bring in >>> static files like CSS. Following the documentation and the "dba" web >>> application that comes with BaseX, I have included the following in the >>> HTML: >>> >>> <link rel="stylesheet" type="text/css" href="static/app.css"/> >>> >>> to point to a file that resides here: >>> >>> ~/basex/webapp/bookplates/static/app.css >>> >>> I have a module sitting here: >>> >>> ~/basex/webapp/bookplates/controller.xqm >>> >>> with a function with these annotations: >>> >>> %rest:path("bookplates") >>> %output:method("html") >>> >>> When I point my browser to http://localhost:8984/bookplates/ the server >>> successfully returns the HTML, but when the browser tries to load the CSS it >>> requests http://localhost:8984/bookplates/static/app.css and the server >>> responds with a 404 saying "No function found that matches the request" as >>> if the server is looking for a RESTXQ function. >>> >>> What am I doing wrong? >>> >>> Thanks, >>> Greg > >
FW
Felix Wolf6 May 2016 17:55 #
Hi Marco, my preferred setup is to use a reverse proxy in front of basex. This would allow you beside other advantages to completely free basex from serving static files. Famous http servers like Apache or NGINX could act as reverse proxies. This is a sample NGINX configuration snippet:
         location ~* ^.+\.(jpg|jpeg|gif|png|css|pdf|js|flv|swf|html|htm)$
         {
                 root   /home/basex/webapp;
         }

         location / {
                 proxy_pass http://127.0.0.1:8984;
         }
I hope this will be no contrifusion :) Felix
Quoted text · Christian Grün · 98 lines
Am 06.05.2016 um 17:23 schrieb Christian Grün: > Hi Marco, > > Does the behavior change if you remove output:method("html")? > > Cheers, > Christian > > >> declare >> %rest:path("/dasbox/ui/resources/{$resource=.+}") >> %rest:GET >> %output:method("html") >> function ui:resources($resource as xs:string){ >> let $path := file:base-dir() || 'resources/' || $resource >> let $mediatype := web:content-type($path) >> return ( >> web:response-header(map { 'media-type': $mediatype }), >> if (matches($mediatype, "^text/.+")) then file:read-text($path) >> else file:read-binary($path) >> ) >> }; >> >> Thanks for your support, >> Marco. >> >> >> On 03/05/2016 02:59, Murray, Gregory wrote: >>> Hi Christian, >>> >>> Thanks for the hints! I see that using webapp/static does work, but I >>> prefer to keep all files related to an app together under one directory, so >>> I'll use the second option you provided -- using RESTXQ. >>> >>> Many thanks, >>> Greg >>> >>> ________________________________________ >>> From: Christian Grün [christian.gruen at gmail.com] >>> Sent: Monday, May 02, 2016 3:20 AM >>> To: Murray, Gregory >>> Cc: basex-talk at mailman.uni-konstanz.de >>> Subject: Re: [basex-talk] Web application static files >>> >>> Hi Greg, >>> >>> Thanks for your mail. If you are working with the ZIP distribution, >>> you could compare your code with the RESTXQ examples in the restxq.xqm >>> file. >>> >>> I assume you get 404 because your static directory is a sub-directory >>> of bookplates. Here are two things you can do: >>> >>> * Move your static directory from "webapp/bookplates" to "webapp" >>> * Deliver static files via RESTXQ (see common.xqm and the REST path >>> "/dba/static/{$file=.+}"). >>> >>> Does this help? >>> Christian >>> >>> >>> >>> On Sun, May 1, 2016 at 6:43 PM, Murray, Gregory >>> <gregory.murray at ptsem.edu> wrote: >>>> Hello, >>>> >>>> I'm new to BaseX, and I'm setting up a simple web application (on >>>> Mac/Unix, using the ZIP distribution of BaseX). I'm unable to bring in >>>> static files like CSS. Following the documentation and the "dba" web >>>> application that comes with BaseX, I have included the following in the >>>> HTML: >>>> >>>> <link rel="stylesheet" type="text/css" href="static/app.css"/> >>>> >>>> to point to a file that resides here: >>>> >>>> ~/basex/webapp/bookplates/static/app.css >>>> >>>> I have a module sitting here: >>>> >>>> ~/basex/webapp/bookplates/controller.xqm >>>> >>>> with a function with these annotations: >>>> >>>> %rest:path("bookplates") >>>> %output:method("html") >>>> >>>> When I point my browser to http://localhost:8984/bookplates/ the server >>>> successfully returns the HTML, but when the browser tries to load the CSS it >>>> requests http://localhost:8984/bookplates/static/app.css and the server >>>> responds with a 404 saying "No function found that matches the request" as >>>> if the server is looking for a RESTXQ function. >>>> >>>> What am I doing wrong? >>>> >>>> Thanks, >>>> Greg >>
ML
Marco Lettere6 May 2016 18:01 #
@Christian I'm so sorry. I don't know how this sneaked into the code and especially how I could overlook it for two hours this morning. Maybe because of Friday ... Of course it works as it should. Thanks. @Felix Yes your pattern sounds meaningful and I often used the approach. Nowadays I'm working on a project that is a small web wrapper on some sort of embedded machine and I have to pay attention to disk usage, amount of processes and so on. Thus, no extra webserver this time. Thanks. Best Regards, Marco.
Quoted text · Felix Wolf · 130 lines
On 06/05/2016 17:55, Felix Wolf wrote: > Hi Marco, > > my preferred setup is to use a reverse proxy in front of basex. > This would allow you beside other advantages to completely free basex > from serving static files. > Famous http servers like Apache or NGINX could act as reverse proxies. > > This is a sample NGINX configuration snippet: > > location ~* ^.+\.(jpg|jpeg|gif|png|css|pdf|js|flv|swf|html|htm)$ > { > root /home/basex/webapp; > } > > location / { > proxy_pass http://127.0.0.1:8984; > } > > I hope this will be no contrifusion :) > > Felix > > > Am 06.05.2016 um 17:23 schrieb Christian Grün: >> Hi Marco, >> >> Does the behavior change if you remove output:method("html")? >> >> Cheers, >> Christian >> >> >>> declare >>> %rest:path("/dasbox/ui/resources/{$resource=.+}") >>> %rest:GET >>> %output:method("html") >>> function ui:resources($resource as xs:string){ >>> let $path := file:base-dir() || 'resources/' || $resource >>> let $mediatype := web:content-type($path) >>> return ( >>> web:response-header(map { 'media-type': $mediatype }), >>> if (matches($mediatype, "^text/.+")) then file:read-text($path) >>> else file:read-binary($path) >>> ) >>> }; >>> >>> Thanks for your support, >>> Marco. >>> >>> >>> On 03/05/2016 02:59, Murray, Gregory wrote: >>>> Hi Christian, >>>> >>>> Thanks for the hints! I see that using webapp/static does work, but I >>>> prefer to keep all files related to an app together under one >>>> directory, so >>>> I'll use the second option you provided -- using RESTXQ. >>>> >>>> Many thanks, >>>> Greg >>>> >>>> ________________________________________ >>>> From: Christian Grün [christian.gruen at gmail.com] >>>> Sent: Monday, May 02, 2016 3:20 AM >>>> To: Murray, Gregory >>>> Cc: basex-talk at mailman.uni-konstanz.de >>>> Subject: Re: [basex-talk] Web application static files >>>> >>>> Hi Greg, >>>> >>>> Thanks for your mail. If you are working with the ZIP distribution, >>>> you could compare your code with the RESTXQ examples in the restxq.xqm >>>> file. >>>> >>>> I assume you get 404 because your static directory is a sub-directory >>>> of bookplates. Here are two things you can do: >>>> >>>> * Move your static directory from "webapp/bookplates" to "webapp" >>>> * Deliver static files via RESTXQ (see common.xqm and the REST path >>>> "/dba/static/{$file=.+}"). >>>> >>>> Does this help? >>>> Christian >>>> >>>> >>>> >>>> On Sun, May 1, 2016 at 6:43 PM, Murray, Gregory >>>> <gregory.murray at ptsem.edu> wrote: >>>>> Hello, >>>>> >>>>> I'm new to BaseX, and I'm setting up a simple web application (on >>>>> Mac/Unix, using the ZIP distribution of BaseX). I'm unable to >>>>> bring in >>>>> static files like CSS. Following the documentation and the "dba" web >>>>> application that comes with BaseX, I have included the following >>>>> in the >>>>> HTML: >>>>> >>>>> <link rel="stylesheet" type="text/css" href="static/app.css"/> >>>>> >>>>> to point to a file that resides here: >>>>> >>>>> ~/basex/webapp/bookplates/static/app.css >>>>> >>>>> I have a module sitting here: >>>>> >>>>> ~/basex/webapp/bookplates/controller.xqm >>>>> >>>>> with a function with these annotations: >>>>> >>>>> %rest:path("bookplates") >>>>> %output:method("html") >>>>> >>>>> When I point my browser to http://localhost:8984/bookplates/ the >>>>> server >>>>> successfully returns the HTML, but when the browser tries to load >>>>> the CSS it >>>>> requests http://localhost:8984/bookplates/static/app.css and the >>>>> server >>>>> responds with a 404 saying "No function found that matches the >>>>> request" as >>>>> if the server is looking for a RESTXQ function. >>>>> >>>>> What am I doing wrong? >>>>> >>>>> Thanks, >>>>> Greg >>> >

⚡Generated with XQuery