Dealing with Velo Web Modules: Our Advanced Tips for Improved Function Import | Hacker Noon

Velo by Wix Hacker Noon profile picture

@veloVelo by Wix

Velo is a full-stack development platform that empowers you to rapidly build, manage and deploy professional web apps.

Web modules are exclusive to Velo and enable you to write functions that
run server-side in the backend, and easily call them in your client-side code. With web modules you can import functions from backend into files or scripts in page code or public files, knowing they will run server-side in the backend. Velo handles all the client-server communication required to enable this access. See the advanced tip below if you want to know how this communication is handled.

Advanced: How Web Modules Work – Behind the Scenes

When you import a web module on the client-side you get a proxy function to the web module function. This proxy function uses an XMLHttpRequest to invoke the function in the backend. The runtime listens to those
invocations and calls the appropriate function.

The arguments and return value are serialized using JSON.

When to Use Web Modules

Web module functions contain code you would typically want or need to run in the backend. Your code may have security risks if it runs in the frontend, or you may want to access other web services. For example, let’s say you want to enable your site visitor to send an email via a third-party provider. You would write your function that sends the email in the backend, like this:

// Filename: backend/sendEmail.jsw  (web modules need to have a *.jsw* extension)

import {fetch} from 'wix-fetch';  
// wix-fetch is the API we provide to make https calls in the backend

const API_KEY = "the api key for the backend service that sends email";

export function sendEmail (address, subject, body) {
 return fetch("" + API_KEY, {
             method: 'post',
             body: JSON.stringify({address, subject, body})
 }).then(function(response) {
  if (response.status >= 200 && response.status < 300)
   return response.text();
   throw new Error(response.statusText);

You need this code to run in the backend for two reasons:

  1. It includes your API key to the third-party service, which you don’t want to expose in the client.
  2. You are calling a third-party web service, which you can only do in the backend because of cross-origin resource sharing (CORS).

You would then import the function from your web module into your frontend file and use it:

import {sendEmail} from 'backend/sendEmail.jsw';

export function sendButton_onClick(event) {
         .then(function() {
            console.log("email was sent");

Using Web Module Functions in Backend

You can import a web module function into another module in backend.

Calling a Function in a Web Module

Unlike regular modules that allow you to export functions, objects, and other items, you can only export functions from web modules. Web modules also always return a promise. This is true even if, in the implementation of the function, it returns a value. For example, if your web module function returns a value, like this:

// Filename: aModule.jsw (web modules need to have a *.jsw* extension)
export function multiply(factor1, factor2) {
    return factor1 * factor2;

When you call the function, it still returns a promise that resolves to the value. So you need to use it like this:

import {multiply} from 'backend/aModule';
multiply(4,5).then(function(product) {
      // Logs: 20

Debugging Web Modules

You can log messages to the console in web modules and they will be
displayed in the client’s console log, even though the code is running in the backend.

Web Module Permissions

Because Web Modules allow you to call your backend code from the frontend, it’s important that you limit which visitors can access their functionality by setting their permissions.

See About Web Module Permissions to learn more.

Previously published at


Join Hacker Noon

Create your free account to unlock your custom reading experience.

read original article here