Require

Node.js provides the require method to load any other files or modules in your app.

When using require to load scripts or Node Modules, it is important to know that you will have to manually stop the server and start it again.

This is because Node.js will include the files at start time, so next time you run it will use the precompiled version.

To prevent this behaviour and improve the performance, Previous.js comes with the load method which ensure any change on the loaded file will be considered in all executions, so you do not need to restart the server.

Important!

The reqirefunction is only available in server side.

Good to know

The special method import is not supported.

Default Node Modules

Previous.js preload these Node.js Modules in all your layout and page scripts:

fs Node.js File System Module
path Node.js Path Module

Then you can access all fs and path methods without require in your scripts. For example:

...

const content = fs.readFileSync('path/to/file.json');

...

const filePath = path.join('/foo', '..', 'bar/asdf.txt');

...

Important!

These objects are only available in server side.

Good to know

Avoid reassign these objects in your scripts or you can find errors or unexpected behaviours.

You can load any other Node.js Modules into your app scripts by using require but remember to restart your server when doing this.

Default Functions

Previous.js will preload next functions in all your layouts, pages and routes scripts:

load Javascript loader (like require)
read Text file template reader
getJSON Convert data to JSON response

Important!

These functions are only available in server side.

Good to know

Avoid reassign these functions in your scripts or you can find errors or unexpected behaviours.

load

This method provide a way to load any script in the server side of your app at runtime and it will be updated if any change is made in the loaded script.

const func = load("libs/func.js");
const obj = load("libs/obj.js");
const Class = load("libs/Class.js");

The loaded script must return a function, an object or a class, then you can use it very similar to require function.

let a = myFunction();
let b = obj.render();
let c = new Class();

To learn more about load function go to Components or Data.

read

The read method provides a simple way to read a text file as string and parse ${props.*} tokens from data provided.

For example you can put an HTML template like this in a file:

<h1 class="${props.class}">${props.title}</h1>

So you can get the parsed content with:

const headerHTML = read("templates/header.html", {
  title: 'The Title',
  class: 'main-title'
});

This method is used to automatic parse page.html or layout.html files in your app/ folder, but you can use it to manually parse other files.

See more about rendering.

getJSON

Use this function to return an object in JSON format with the corresponding content type application/json to use as the final result returned by APIs.

return {
  GET: (props) => {
    return getJSON({
      id: 1,
      title: 'Hello'
    });
  }
}

Learn more about creating APIs.