Function Repository Resource:

WebServerDeploy

Source Notebook

Deploy an expression to a local web server

Contributed by: Christopher Wolfram

ResourceFunction["WebServerDeploy"][expr]

deploys expr to a local web server.

ResourceFunction["WebServerDeploy"][expr,loc]

deploys expr to the address or port specified by loc.

Details and Options

Servers deployed with WebServerDeploy respond to HTTP requests.
expr can contain cloud deployable objects including APIFunction,ExportForm,HTTPResponse, etc.
WebServerDeploy generates responses with GenerateHTTPResponse, producing analogous results to expressions deployed with CloudDeploy.
Evaluations triggered by requests to the server generally run in the same kernel from which the server was deployed.
loc takes the same input format as the first argument of SocketListen.
WebServerDeploy returns a SocketListener.
The SocketListener returned by WebServerDeploy can be closed with DeleteObject.
WebServerDeploy supports the following options:
HandlerFunctions<||>how to handle events generated
HandlerFunctionsKeysAutomaticwhat parameters to supply to handler functions
When an HTTP request is made to the webserver, the following events can be generated:
"HTTPRequestReceived"request received
"HTTPResponseSent"response sent
Possible keys specified by HandlerFunctionsKeys include:
"HTTPRequest"the HTTPRequest
"HTTPResponse"the HTTPResponse

Examples

Basic Examples (3) 

Deploy a web server hosting a simple API:

In[1]:=
server = ResourceFunction["WebServerDeploy"][
  APIFunction[{"x" -> "Integer"}, #x + 3 &]]
Out[1]=

Make a request to the API:

In[2]:=
resp = URLRead[<|"Scheme" -> "HTTP", "Domain" -> First[server["Socket"]["DestinationIPAddress"]], "Port" -> server["Socket"]["DestinationPort"], "Query" -> {"x" -> 12}|>]
Out[2]=
In[3]:=
resp["Body"]
Out[3]=

Stop the web server:

In[4]:=
DeleteObject[server]

Scope (3) 

Deploy a simple website with pages and APIs:

In[5]:=
server = ResourceFunction["WebServerDeploy"]@URLDispatcher[{
    "/home" -> ExportForm[
      Column[{Style["Welcome!", 24], "This is a simple page"}], "HTML"],
    "/api" -> APIFunction["x" -> "String", StringReverse[#x] &]
    }]
Out[5]=

The root page is a 404:

In[6]:=
WebImage[
 URLBuild[<|"Scheme" -> "HTTP", "Domain" -> server["Socket"]["DestinationIPAddress"], "Port" -> server["Socket"]["DestinationPort"], "Path" -> {}|>]]
Out[6]=

The homepage and API load at their respective paths:

In[7]:=
WebImage[
 URLBuild[<|"Scheme" -> "HTTP", "Domain" -> server["Socket"]["DestinationIPAddress"], "Port" -> server["Socket"]["DestinationPort"], "Path" -> {"home"}|>]]
Out[7]=
In[8]:=
WebImage[
 URLBuild[<|"Scheme" -> "HTTP", "Domain" -> server["Socket"]["DestinationIPAddress"], "Port" -> server["Socket"]["DestinationPort"], "Path" -> {"api"}|>]]
Out[8]=

Open the root page in a browser:

In[9]:=
SystemOpen@
 URLBuild[<|"Scheme" -> "HTTP", "Domain" -> server["Socket"]["DestinationIPAddress"], "Port" -> server["Socket"]["DestinationPort"]|>]

Close the web server:

In[10]:=
DeleteObject[server]

Deploy a webserver to a specific port:

In[11]:=
server = ResourceFunction["WebServerDeploy"][
  APIFunction[{"x" -> "Integer"}, #x + 3 &], 18000]
Out[11]=
In[12]:=
URLRead["http://localhost:18000?x=12", "Body"]
Out[12]=

Close the web server:

In[13]:=
DeleteObject[server]

Deploy an API that modifies local state:

In[14]:=
localVariable = {};
In[15]:=
server = ResourceFunction["WebServerDeploy"][
  APIFunction[{"x" -> "String"}, AppendTo[localVariable, #x] &], 18000]
Out[15]=

Make calls to the web server:

In[16]:=
URLRead["http://localhost:18000?x=Hello", "Body"]
Out[16]=
In[17]:=
URLRead["http://localhost:18000?x=World", "Body"]
Out[17]=

The local state has been modified:

In[18]:=
localVariable
Out[18]=

Close the web server:

In[19]:=
DeleteObject[server]

Options (4) 

HandlerFunctions (4) 

Use HandlerFunctions to add logging to a web server:

In[20]:=
reqs = {};
In[21]:=
server = ResourceFunction["WebServerDeploy"][
  APIFunction[{"x" -> "Integer"}, #x + 3 &], HandlerFunctions -> <|"HTTPResponseSent" -> (AppendTo[reqs, #] &)|>]
Out[21]=

Make requests:

In[22]:=
Do[URLRead[<|"Scheme" -> "HTTP", "Domain" -> First[server["Socket"]["DestinationIPAddress"]], "Port" -> server["Socket"]["DestinationPort"], "Query" -> {"x" -> RandomInteger[{1, 10}]}|>], 3]

View the request logs:

In[23]:=
reqs
Out[23]=

Close the web server:

In[24]:=
DeleteObject[server]

Publisher

Christopher Wolfram

Version History

  • 1.0.0 – 05 April 2023

Related Resources

Author Notes

There are some known issues with FormFunction.

License Information