SaaSBox
Search
K

Making API Requests to your Application

Step 2: Make the API request to your Backend API in the SaaSBox dashboard:
In your github repository, view the users.pug file by first invoking the web editor with the "." key pressed (this is described in the github frontend setup guide)
Hint: Regular users are by default served the user.pug file under /app/start. Admin users are by default served the admin.pug file. If neither of the files are found, main.pug file is served.
Let us take a look at the user.pug file:
1
html
2
include common/app-head.pug
3
+head("Welcome to ExpressJS API Example")
4
body
5
.min-h-full
6
include common/app-navbar.pug
7
+app-navbar()
8
div
9
header
10
main
11
//- JWT that identifies this user:
12
div(hidden, id="token", data-token=token)
13
.max-w-full.mx-auto
14
// Replace with your content
15
include ./common/app-footer.pug
16
+footer()
17
script(src=asset('javascripts/fetchUserData.js'))
18
script.
19
// Fetch the current user or create it if it doesn't exist.
20
$(document).ready (async function(){
21
await createGetUser();
22
});
The dashboard frontend file is as simple as above. Some notable details:
  • At line 12, there is a token element embedded in a hidden div. This is the JWT token that identifies the current user. This token can be decoded at the server side using the JWT secret we added in the environment variables for this application.
  • At line 21, there is a call to createGetUser(). This function sends the JWT to the server and returns the user data. For example you may be saving additional application data related to this user on the server side.
Let us take a look at the fetchUserData.js file that is defined at line 17:
1
const createGetUser = async function() {
2
let jwt = $("#token").attr("data-token");
3
let server_url = "https://expressjs-api-backend.onrender.com";
4
return new Promise((resolve, reject) => {
5
$.ajax({
6
url: server_url + "/create-get-user",
7
headers: { Authorization: "Bearer " + jwt },
8
contentType: 'application/json; charset=utf-8',
9
dataType: 'json',
10
data: JSON.stringify({}),
11
type: 'POST',
12
success: ((res) => {
13
let user_data = res.user_data;
14
console.log("User data received:", user_data);
15
resolve(user_data);
16
}),
17
error: ((err) => {
18
console.log(err.responseJSON.error);
19
reject({ error: err.responseJSON.error });
20
})
21
});
22
});
23
}
  • This function retrieves the JWT token embedded in the hidden div, and makes a POST request to the create-get-user API endpoint defined at the API server, set by the server_url parameter.
  • The success case returns the user data along with any application specific data tied to this user.
  • Using the data returned by this initial call upon the page load, one can populate the current page with application specific data.
This is a simple method for loading dynamic data on each dashboard page, without using any complex frontend frameworks such as React or others. It works well for applications that use APIs that accomplish valuable work for the user, but need simple frontend logic.