Web modules allow you to write backend functions that you can easily call from the frontend. The following describes how to create, export, and call web module functions, also known as web methods.
The way you add a web module file depends on which IDE you're using.
This action creates a web module file with a .web.js
extension.
Create a new file in the backend-modules
directory and name it with the .web.js
extension.
Add an exported web method in the file you created above. The web method wraps an inner function that contains the logic you want to call from the frontend.
To create a web method, call the webMethod()
function and define the web method's permissions and its inner function:
Add the necessary imports:
import { Permissions, webMethod } from "wix-web-module";
Call the webMethod()
function and store its returned value in an exported variable.
export const myFunction = webMethod();
Pass the webMethod()
function a permissions value as the first argument:
export const myFunction = webMethod(Permissions.SiteMember);
Pass the webMethod()
function an inner function as the second argument:
export const myFunction = webMethod(Permissions.SiteMember, (someParam) => {
// Some functionality for site members to call from the frontend
return `You passed me ${someParam}`;
});
To call a web method from the frontend:
Import the exported web method from the web module you created above:
import { myFunction } from "backend/weather.web";
Call the imported function:
const fromBackend = await myFunction(someValue);
Remember, web module functions are always asynchronous.