What are we going to build?
In this tutorial, you’ll learn how to create custom scripts and libraries, and how to share them with other users. You’ll also learn about predefined JS libraries, and we’ll touch on security as well.
Before you begin
Tutorial level: intermediate.
Prerequisites: an Appery.io account.
Creating a script and library
1. Open an existing project or create a new one. Open the Server Code
tab.
2. To the left, you should see the list of scripts and libraries. Click “Create script”:
3. Name it scriptWithLibrary
and click “Save”.
If successful, such operation will be notified of:
Now, you’ll create a library. We’ll use the popular JavaScript library, underscore.
1. Click the Server Code
tab, and then click “Create library”. Name it underscore
:
2. In your browser, open underscorejs.org. On this site, find Downloads
and then find Production version
. Click it, and then copy the code.
3. Return to your project. In the code area, insert the copied code. Click “Save”:
The message ”Library saved” appears.
Now we’ll add our script to the newly created library.
1. Open your scriptWithLibrary
script, and go to the Dependencies
tab. You can see the list of all existing libraries. To connect to the added library, just check the box labeled “underscore”and you will get the message:
2. Go to the Script
tab.
3. To be sure that our library works, let’s add some code and invoke a function from the library. For example, type:
1 2 |
var array = [1, 2, 3, 4, 5]; console.log(_.first (array)); |
This function should return the first element of the “array”:
4. Click “Save”.
5. Click “Save and run” on the right of the pane:
Our script has been successfully executed (you should see status 200):
Now, we can see the result – the first element of array:
Sharing
In this part of the tutorial, we’ll learn how to share scripts and libraries with other users.
1. Open the underscore
library.
2. Go to the Permissions
tab.
3. Input the email(s) of the user(s) you want to share with, and click “Add”:
4. Open your script, open the Permissions
tab, and perform the same actions.
Now, another user has access to the shared script and library.
Here you can find more information on managing team permission rights.
Predefined JS libraries
Every user has a set of predefined JavaScript libraries. They’re marked with the label system library
. The user can also see the version of the library he/she is going to use:
Remember, if you have read-only rights, you CANNOT:
- Delete or rename a system library.
In the description section, you’ll find useful information – homepage URL
:
Let’s take a look at how this all works.
First, create a new script with the predefined system library Handlebar.js
.
1. Create the script. Name it greeting
and, in the code area, enter:
1 2 3 4 5 6 7 8 9 10 |
var textTemplate = 'Hello {{user.nickname}} from {{user.city}}'; var template = Handlebars.compile(textTemplate); var data = { "user": { "nickname": "Exadel", "city": "Minsk" } }; var greetings = template(data); response.success(greetings, "plain/text"); |
2. Click “Save”.
3. Go to the Dependencies
tab, and select the “Handlebars.js” checkbox:
Now let’s execute our script:
1. Go to the Script
tab, and click “Save and run”. You should see:
You may have noticed the Alias
input under the REST information
tab:
Alias
is a human-friendly synonym for the script, which can be used instead of “greeting.”
2. In the associated input field, type the alias name (e.g. greetings). Click “Save“.
The alias name should be unique.
Compare – before using alias:
1 2 |
Execute URL: https: //api.appery.io/rest/1/code/<guid>/exec |
And after:
1 2 |
Execute URL: https: //api.appery.io/rest/1/code/greetings/exec |
Now, you have a shorter and friendlier name for your script. You can use this URL in your REST
service:
Security
It’s possible to restrict access to your JavaScript:
1. Open your script. Go to the Settings
tab.
2. Select a database for managing user access (you should have a database with at least one user. Find more information about this here or here).
3. Check “Allow only authenticated users to call this script”.
The option “Share with support” is placed at the bottom of the page.
When a user shares (turns the toggle
“on”), the specified script appears in the admin section. This feature can be used by the support team for viewing and accessing all resources (apps, databases, server scripts, etc) shared:
When a user unshares (turns the toggle
“off”), the specified resource can’t be accessed for viewing by the support.
4. Go back to the Script parameters
tab. You should see the session token input:
5. Enter an access token or click “Obtain”.
6. In the popup, enter the username and password of the user you want to give access credentials:
7. Click “Obtain”.
You should see the token.
Running it without a token:
To test the script, under the Run
tab, click “Save and run” again:
Trying to execute the script without a session token:
Clear the access token (click “Clear”), switch to the Run
tab and run the script again. You should see an “Access denied” error:
You can find more information about adding custom server code here.