Ionic 5 Quickstart Server Code Tutorial

Server Code quick start tutorial that demonstrates how to creating your first Ionic 5 app backend.

Introduction

If you are new to Server Code, this is a good place to start.

Server Code allows writing custom app code using JavaScript that runs (executes) on the Appery.io server. The code is then used as the app backend. In this tutorial, you are going to create a backend for a greeting app. From the app (client) you will be able to enter a name, invoke the Server Code script (backend), and the backend will return a greeting, including the current time.

Let's start by creating the Server Code script.

Creating Server Code Script

The first step in this tutorial - you are going to create and test the Server Code script.

  1. Open the Server Code page.
  2. On the left side, click the Create script button. The script editor will open.
  3. For the Name field, enter greetingScript and click Save.

What's nice is that a sample script is already there and that's the below script you are going to use, just copy it and paste into the code editor field:

Let's quickly review the script code. The code looks like this (without comments):

var devName = request.get("devName");
var greeting = "Hello " + devName + "!";
var time = new Date().toString();
response.success({"result":greeting, "time":time}, "application/json");

The script takes the name (or developer name) as the input into the script. In the first line, the script reads the parameter value devName.

📘

Script REST API

A Server Code script is automatically exposed via a REST API. This means to invoke a script from an app you will simply be invoking a REST API. Inputs into the script are simply REST API parameters.

On line #2, the greeting variable is created by by combining Hello and the developer name.
On line #3, the time variable is set by invoking a standard JavaScript Date API.
On line #4, the script response is created. This is the response returned by the REST API when the script is invoked.

That's all for the script. Next, you are going to test the script.

Testing Script

👍

It's always a good idea to test your backend (here, script) before using it in the app. This way you can debug the logic without invoking it from the app. Debugging the script from an app is more difficult.

Testing the script in Server Code is simple. Go to the Run tab and click the Save and Run button. You should see output that looks like this:

You got the response from the script and almost everything looks good, but the greeting label doesn't have any name. Remember that the name is passed as a parameter into the script. Any time the script accepts a parameter, you can add the parameters in the Script parameters tab for testing.

  1. Switch to the Script parameters tab.
  2. Set the Request method to POST. This means the script will be invoked via the GET request from the app.
  3. Under Request params, enter devName and click Add.
  4. Enter any test value for devName, for example: John:
  1. Switch back to the Run tab and click the Save and run button to test the script again. You will see an updated response, now with the name:

This means that the script works as expected.

Now that the script has been tested, the next step is to create an app that will use this script.

Building App

We will now build a one page app that will invoke this script.

The first step is to create a new app.

Creating New App

  1. Navigate to the Appery.io dashboard. Here, from the Apps tab, click Create new app.
  2. Select the Ionic 5 application for its type and Blank as a template. Name it Ionic 5 Greeting App and confirm by clicking Create:

The App Builder will load the newly created app.

Next, you are going to import the script into the app.

Importing Server Code Script

The script is imported as a REST API into the app.

  1. Select CREATE NEW > Server Code Service.
  2. Select the script you created and then click the Import selected services button:

You should see the script under the Services folder:

  1. Click the script to open it and then go to its Request > Query String tab. You will see that John is set, that's the value you used to test the script. Delete this name, otherwise, it will be used as the default value for the service:
  1. Switch to the Test > Query String tab. This is where you can test the REST API (script) inside the App Builderr. Enter any name value and click the Test button. You should see the same response as when you tested the script in Server Code:
  1. Click the Import as Response button to define a response for this service:

You can switch to the Response tab to see the response created:

📘

Want to know more?

Now it's time to design the page.

Building Page UI

In this step, you are going to build the app page.

  1. Every new app comes with a default Screen1 page. Let's open it: Pages > Screen1 and under the page PROPERTIES panel, set the Page Footer property to False as we won't need the bottom app toolbar:
  1. Click the Toolbar title in the app screen and change it to Greeting App by changing the Text property on the right PROPERTIES panel menu:
  1. Drag and drop the Input component.
    Under the PROPERTIES panel, perform the following:
    • Set the Component name property to nameInput;
    • Expand the Label property and set its Text value to Name;
    • Set the Placeholder value to Enter developer name.
  2. Drag & drop the Button component below the input. Change its Text property to Say Hello.
  3. Now, drag and drop the Grid component to the Content area and place it under the button. The grid will be created nesting a default Grid Row with two child Grid Cell components.
  4. Drag & drop the Text component to both cells. For both, clear the default Text property to make them blank. And set their Container property to p or div.
  5. Select the left text and set its Component name property to greetingText.
  6. Now, select the right text and set its Component name property to timeText.:

This is for the page. You can test the page by clicking the TEST button in the App Builder Toolbar:

The next step is mapping and invoking the service from this page so that the response could include the developer name.

Mapping Service

In this step, you are going to add the service to the page, map it, and then invoke it.

  1. Switch to the DATA panel.
  2. For the datasource, select greetingScript_service, click Add.
  3. Rename it to greetingService:
  1. Now let's create the input mapping. Click the Mapping button for the Before send event.
  2. Click the Expand all links on both sides of the mapping editor.
  3. Using drag & drop, map nameInput > Value from the left panel to the query devName in the right panel.
  4. Click Save & Replace:
  1. Click Mapping for the Success event, then click the Expand all links on both sides.
  2. Again, using drag & drop, map the body result property from the left panel to the Text property of the greetingText in the right panel.
  3. Similarly, map the body time property to the Text property of the timeText component.
  4. Click Save & Replace:
  1. Go back to the DESIGN panel.
  2. Select the button and open the EVENTS tab (bottom). This is where you will add an event to invoke the service.
  3. Make sure that the button is selected and the EVENT is set to Click. From the ACTION drop-down, select Invoke service > greetingService, click Save:
  1. Save all the changes.

You are now ready to test the app.

Testing App

Click the TEST button in the App Builder Toolbar to launch the app in the browser and try entering different names:


Did this page help you?