Skip to content

Code Reuse, Imports, Exports and Lib Files in MARS Engine

MARS Engine supports modular, maintainable back-end code by letting you split logic into reusable files. You can export functions or data from one script and include them in another.

Exports ⇒

Use the exports object to share data or functions from a file.

Example utility file:

js
// some_util_file

exports.requiredParameter = function(paramName) {
  write("RES", "ERR");
  write("MSG", "Parameter " + paramName + " is missing");
  exit();
}

✅ Notes:

  • You must assign named functions to fields on exports.
  • This won't work:
js
exports.requiredParameter(paramName) { ... }
  • But arrow syntax is fine:
js
exports.requiredParameter = (paramName) => { ... }

Including ⇐

To use exports in another API, use the include() function.

js
const util = include("some_util_file");

let name = param("name", util.requiredParameter);

write("RES", "OK");
write("MSG", "Hello " + name + ". How are you today?");

✅ Notes:

  • include() always resolves paths from the project root.
  • For example:
js
const util = include("shared/some_util_file");
  • The imported object will contain all exported fields.

Example: Validating Parameters

Without validation, you risk this:

json
{
  "RES": "OK",
  "MSG": "Hello undefined. How are you today?"
}

Using your util:

js
const util = include("some_util_file");
let name = param("name", util.requiredParameter);

Now missing name will trigger:

json
{
  "RES": "ERR",
  "MSG": "Parameter name is missing"
}

Init Scripts

For truly global code shared everywhere, mark a file as an Init Script in your project settings.

In your file:

js
function checkParam (param, paramName) {
    if (!param) {
      write("RES", "ERR");
      write("MSG", "Parameter " + paramName + " is null");
    }
}

Then use anywhere:

js
let test = param("test", null);
checkParam(test, "test");

Output:

json
{
  "MSG": "Parameter test is null"
}

Preventing code from being accessible

If your file (API, Library, Text, HTML, CSS, image ...) or Folder has a "." at the beggining of it's name it will not be retrievable using HTTP requests.

Folder that is marked as inacessible also makes all the files and subfolders within inacessible.

This is useful when you want to have files that you can reuse, but without having them accessible on the internet.

Meaning that you can only use it via include or fs within your APIs - anyone requesting that particular file from your server will get a 404 HTTP - File not found.

For example:

project-root/
├── api/
│   ├── hello-world
│   └── .private-code

curl api/hello-world would return a response.

curl api/.private-code would return a 404.

Library (LIB) files

When creating a file on MARS Engine, you can create an API, Lib or a text file (which can then be html/css/js/json/xml ...).

Library files are functionally the same as API files in the sense that they are executable code, but they cannot be executed via HTTP requests like API files. Instead they are meant to be used as reusable blocks of code which can export functions and variables to be used in other files.

Yes, you can include Lib files within other Lib files.

project-root/
├── api/
│   ├── greet-user
│   └── ...
│   └── shared/
│       └── some_util_file
└── ...

Use include("api/shared/some_util_file") to load reusable utilities.

Use Init Scripts for global shared objects.