Writing a Hello World Service

JavaScript

The Mashup Server supports the quick and easy creation of Web services written in JavaScript. Just as a Web server such as Apache or IIS publishes HTML and other Web content from a local directory to the Web, the Mashup Server takes JavaScript files, placed in a local directory, and exposes the functions as Web Service operations.

The simplest way to deploy a new Web service is simply to write some JavaScript and place it in the scripts directory which is in the repository directory of your Mashup Server installation. Alternatively you can use the Create new service wizard from the UI or just upload the JavaScript file from the UI. Follow these simple steps:

Step 1:

In Notepad or your favorite IDE, type the following text:

                function hello() {
                    return "Hello World";
                }
            

Step 2:

Save this file as 'helloworld.js' in the a directory(you can use username or any name as the directory name and this directory can contain multiple services) within the repository/deployment/server/jsservices directory of the Mashup Server.

Step 3:

That's it, the helloworld service is now live (probably a few seconds later just after the file is placed in the scripts directory), exposing a single operation 'hello' that returns the text "Hello World". But let's try it out to make sure it's working properly. Open up the WSO2 Mashup Server web-based console (typically at https://localhost:9443/carbon) and look for the helloworld mashup you just created. It will be listed under Service Listing page else you can use the searching facility of the Mashup Server to look for the helloworld mashup.

hello world service listed in available mashups

Click the 'Try this service' link or try typing the tryit URL (typically at http://<your-machine-ip>:9763/services/helloworld?tryit) in the browser, and you will get a web-based client that can interact with the Web service. In this case just click the button titled hello >> to invoke the hello operation. The response is illustrated below:

The Helloworld mashup also contains many helpful resources which are described here.

Adding an user Interface for your service

When a service is deployed, Mashup server automatically creates a folder to store the resources that belong to the service. This folder is named as "{service javascript file name}.resources". You can install a custom user interface for your service by creating a folder named "www" under the above mentioned resources folder and placing the necessary files under the created "www" folder. This user interface will appear when you point your browser to the Endpoint Reference of the service (eg: http://<your-machine-ip>:9763/services/helloworld).

Please refer to "writing hello world client" for more information about creating AJAX clients for the mashup services deployed in the WSO2 mashup server.