GET/POST Strings when serving a static web page with Rook

29 views
Skip to first unread message

greg riddick

unread,
Aug 1, 2014, 5:51:52 PM8/1/14
to rr...@googlegroups.com
Hi All,

I am trying to use Rook to first:

1)Serve a static web page in an HTML file
2)After that static page is loaded in the browser, then have javascript within the web page retrieve info from Rook via GET and POST


Serving up a static web page works fine (code below) but then how do 
I find the http address for constructing the GET/POST commands?

A query string like this does not seem to work:


The issue is how to get the address of the running "script" that will answer a get/post query?
My understanding is that normally the script location string will be given by the
variable (env$SCRIPT_NAME) that is defined in the Rook app function, but
how to access this when serving a static HTML page? Any insights would be appreciated.

Thanks,
Greg

#######Code#####################################

## Use a Builder to add a 
staticApp <- Builder$new( 
    Static$new(
        urls=c('/app', '/app/library'),
        root = file.path(getwd(), "..", "..") 
        ),
    App$new(function(env) {
        req <- Request$new(env)
        res <- Response$new()
        getstr <<- paste(capture.output(str(req$GET()),file=NULL),collapse='\n')
        res$finish()

})
)


s <- Rhttpd$new(); # Create server
s$add(app=staticApp, name="static")
s$start(port=9000)

greg riddick

unread,
Aug 7, 2014, 12:34:32 PM8/7/14
to rr...@googlegroups.com
Here is solution I figured out:


library(Rook)
library(rjson)

R.server = Rhttpd$new(); # Create server

staticApp = Static$new(root=getwd())

jsonApp = function(env){
   request = Request$new(env)
   response = Response$new()
   
   # Proof of concept creating a JSON document from Get string
   getstr = paste(capture.output(str(request$GET()),file=NULL),collapse='\n')
   response$header('"Content-Type": "application/json"')
   ret = paste(toJSON( getstr ),"\n", sep="" );
   response$write(ret)
   response$finish()
}   


R.server$add(app=staticApp, name="static")
R.server$add(app=jsonApp, name="json")
R.server$start(port=9000)





#R.server$stop()

greg riddick

unread,
Aug 7, 2014, 12:43:02 PM8/7/14
to rr...@googlegroups.com
For the previous example, JSON requests can be
made by the client directed to the URL "/custom/json"


On Friday, August 1, 2014 5:51:52 PM UTC-4, greg riddick wrote:

stephan...@freemavens.com

unread,
Mar 13, 2015, 11:28:46 AM3/13/15
to rr...@googlegroups.com
Thanks, that was really helpful!
Reply all
Reply to author
Forward
0 new messages