Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
C++

RAD Server: Mapping Web Properties to Folders

In my Embarcadero Academy RAD Server courses, “Using Delphi and RAD Server to Rapidly Design, Build, Debug, and Deploy Services-Based Solutions” and “Using C++Builder and RAD Server to Rapidly Design, Build, Debug, and Deploy Services-Based Solutions“, i show Delphi and C++Builder developers how to integrate code with JavaScript, HTML, images, CSS, and other web asset files in an application’s resource endpoints.

The RAD Server configuration file (emsserver.ini) section labeled [Server.PublicPaths] contains information and examples for setting up public paths of web content. Server Public Paths entries have a unique arbitrary name that precedes the equal sign. The JSON based entries map a computer directory to the types of web information that a RAD Server application can use. Each entry contains a “path”: “value”, directory where files are found, optional default file to dispatch upon browsing, optional array of MIME file type masks, optional array of file extensions, and optional character set encoding for the files in the directory.

For an example, with the following path entries to the RAD Server configuration file in the [Server.PublicPaths] section:

Path1={“path”: “images”, “directory”: “C:\WebAssets\images\”, “default”: “index.html”, “mimes”: [“image/*”]}
Path2={“path”: “content”, “directory”: “C:\WebAssets\content\”, “default”: “index.html”, “extensions”: [“js”, “html”, “css”]}

Create the C:WebAssetsContent and C:WebAssetsImages directories. Create an index.html file in the C:WebAssetsContent directory containing the following HTML:

<!DOCTYPE html>
<html>
<head>
<script src=”https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js”>
</script>
<script>
$(document).ready(function(){
$(“button”).click(function(){
$.ajax({url: “/WebProperties”, success: function(result){
$(“#div1”).html(result);
}});
});
});
</script>
</head>
<body>
<h2>RAD Server JS + Service</h2>
<button>Get RAD Server Content</button>
<P></p>
<div id=”div1″>RAD Server GET method result will appear here</div>
</body>
</html>

Add the following code for the implementation of the Get and GetItem endpoints.

Fullscreen

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Delphi:
unit ResourceUnit;
// EMS Resource Module
interface
uses
System.SysUtils, System.Classes, System.JSON,
EMS.Services, EMS.ResourceAPI, EMS.ResourceTypes;
type
[ResourceName(‘WebProperties’)]
TWebPropertiesResource1 = class(TDataModule)
published
procedure Get(const AContext: TendpointContext;
const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
[ResourceSuffix(‘{item}’)]
procedure GetItem(const AContext: TendpointContext;
const ARequest: TEndpointRequest; const AResponse: TEndpointResponse);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Compile your RAD Server project and click the Open Browser button using localhost:<port#>/Content for the URL. This will bring up the index.html file in the browser.

Click the button to call the Get endpoint and see the result:

You can sign up for my RAD Server Embarcadero course to watch more than 3 hours of instruction, read the course notes and try the example projects. You’ll need an Enterprise, Architect or trial edition of RAD Server, Delphi or C++Builder.

Happy Programming!!!

David I.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES