# Running Backend Server

To collect code coverage for a Node.js backend application, the SeaLights Node.js agent can be attached in one of two ways:

* Non-Intrusive Approach (Using `preload.js` with `NODE_OPTIONS`)
* Wrapper Command (Using `slnodejs run`)

{% hint style="success" %}
Once your application starts with the agent, it should appear under **Live Agents Monitor** in the SeaLights Cockpit.
{% endhint %}

## Non-Intrusive Approach (Using `preload.js` with `NODE_OPTIONS`)

This method is ideal when you **cannot or prefer not to modify the application’s execution command**, such as in Docker containers, `pm2`, or CI environments.

The SeaLights agent provides a `preload.js` script that can be injected into the process using the `NODE_OPTIONS` environment variable.

```bash
export SL_tokenfile=./sltoken.txt
export SL_buildsessionidfile=buildSessionId
export SL_labid=my_lab_id
export NODE_OPTIONS="--require ./node_modules/slnodejs/lib/preload.js"

node ./server/app.js
```

> 💡 **Note**: This method also works with TypeScript apps run via `ts-node`, e.g., `ts-node ./server/app.ts`. No special handling is needed beyond setting `NODE_OPTIONS` correctly.

## Wrapper Command (Using `slnodejs run`)

This method wraps the application’s startup command using the SeaLights CLI runner. It gives you full control over parameters and visibility into agent behavior.

### Generic Node.js App

{% tabs %}
{% tab title="Unix/Linux" %}
{% code overflow="wrap" %}

```bash
npx slnodejs run --tokenfile ./sltoken.txt --buildsessionidfile buildSessionId --labid my_lab_id --workspacepath ./server --useinitialcolor true -- ./server/app.js
```

{% endcode %}
{% endtab %}

{% tab title="PowerShell" %}
{% code overflow="wrap" %}

```powershell
npx slnodejs run --tokenfile .\sltoken.txt --buildsessionidfile buildSessionId --labid my_lab_id --workspacepath .\server --useinitialcolor true -- .\server\app.js
```

{% endcode %}
{% endtab %}
{% endtabs %}

### TypeScript App Using `ts-node`

SeaLights only supports `ts-node` apps when started with: `node -r ts-node/register ./server/app.ts`

Wrap this format with the agent:

{% tabs %}
{% tab title="Linux" %}

<pre class="language-bash" data-overflow="wrap"><code class="lang-bash"><strong>npx slnodejs run --tokenfile ./sltoken.txt --buildsessionidfile buildSessionId --labid my_lab_id --workspacepath ./server --useinitialcolor true --useslnode2 -- -r ts-node/register ./server/app.ts
</strong></code></pre>

{% endtab %}

{% tab title="Powershell" %}
{% code overflow="wrap" %}

```powershell
npx slnodejs run --tokenfile .\sltoken.txt --buildsessionidfile buildSessionId --labid my_lab_id --workspacepath .\server --useinitialcolor true --useslnode2 -- -r ts-node/register .\server\app.ts
```

{% endcode %}
{% endtab %}
{% endtabs %}

## Additional Notes

To verify the agent is running, you can run the following command:

{% tabs %}
{% tab title="Linux" %}

```sh
ps -ef | grep slnodejs
```

{% endtab %}

{% tab title="PowerShell" %}
{% code overflow="wrap" %}

```powershell
Get-WmiObject Win32_Process | Where-Object { $_.CommandLine -like "*slnodejs*" } | Select-Object CommandLine, ProcessId
```

{% endcode %}
{% endtab %}
{% endtabs %}

* We recommend using the `--labid` parameter (or `SL_labid` for preload) to correlate your test and listener sessions. If `labid` is not set, the session ID will be used automatically.
* For full parameter details, see the Node.js Command Reference.
