Starting programmatically

You can start Molid programmtically from inside your application or test:

1
2
3
4
5
import { start } from 'molid';

async function your_code() {
    const molid = await start();
}

The code above will start Molid using a random available port and import data from the default data directory.

Start parameters

You can pass a configuration object to the start function:

1
2
3
4
5
6
7
8
import { start } from 'molid';

async function your_code() {
    const molid = await start({
        port: 3456,
        dataDir: path.join(__dirname, '.fake-data'),
    });
}
Parameter Description
port The port to start Molid on
dataDir Absolute path to the data directory

Construct an absolute URI

When starting Molid, you probably want to fetch something from it, and therefore need the URI of a document. When Molid starts on a random port, you will not know the URI. But even if you defined the port yourself, the following function is still handy to construct absolute URIs referring to Molid resources:

1
2
3
4
5
6
7
8
import { start } from 'molid';

async function your_code() {
    const molid = await start();
    const webId = molid.uri('/person/card#me');
    const response = await fetch(webId);
    // ...
}

Stopping Molid

To stop a running Molid instance, call the stop method on the object returned from start

1
2
3
4
5
6
7
import { start } from 'molid';

async function your_code() {
    const molid = await start();
    // do your things ...
    molid.stop();
}

Note

You can start multiple Molid instances at once on different ports.