> For the complete documentation index, see [llms.txt](https://docs.viesus.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.viesus.com/reference/node.js-module/getting-started.md).

# Getting Started

{% hint style="info" %}
**Linux Only**

The VIESUS Node.js module is available on **Linux only**.
{% endhint %}

The module uses a worker thread pool pattern. The main thread dispatches enhancement jobs to a pool of workers; each worker holds one persistent `MyViesusObject` instance initialized with your GUID.

***

## The two files you need

### `worker.js`

Each worker thread initializes VIESUS once on startup and then processes jobs as they arrive:

```js
const { parentPort, workerData } = require('worker_threads');
const viesus = require('viesus');

// Initialize once — workerData is the GUID passed from the pool
const viesusObj = new viesus.MyViesusObject(workerData);

parentPort.on('message', (job) => {
  const result = viesusObj.Enhance(
    job.fromPath,   // input image — absolute path
    job.toPath,     // output image — absolute path
    job.iniPath,    // viesusini.json — absolute path
    job.resPath     // result JSON — absolute path
  );
  // result > 0: processing time in ms
  // result < 0: error code (see API Reference)
  parentPort.postMessage(result);
});
```

### `testbatch.js`

The main thread creates the pool and dispatches all images in a folder:

```js
const { StaticPool } = require('node-worker-threads-pool');
const fs = require('fs');
const path = require('path');

const guid = 'YOUR-GUID-HERE';
const nGPUs = 0; // set to number of available NVIDIA GPUs; 0 = use CPU threads

const nCPUThreads = Number(process.env.UV_THREADPOOL_SIZE);
const nWorkers = nGPUs > 0 ? nGPUs : nCPUThreads;

console.log(`Workers: ${nWorkers}, GUID: ${guid}`);

const pool = new StaticPool({
  size: nWorkers,
  task: './worker.js',
  workerData: guid
});

async function enhance(fromPath, toPath, iniPath, resPath) {
  const result = await pool.exec({ fromPath, toPath, iniPath, resPath });
  console.log(result > 0 ? `OK ${result}ms: ${fromPath}` : `ERR ${result}: ${fromPath}`);
}

const imageFolder = './in';
const outputFolder = './out';
const iniPath = path.resolve('./viesusini.json');

fs.readdir(imageFolder, (err, files) => {
  if (err) process.exit(1);
  files.forEach((file) => {
    const fromPath = path.resolve(imageFolder, file);
    const toPath = path.resolve(outputFolder, file.replace(/(\.[^.]*)?$/, '_viesus.jpg'));
    const resPath = path.resolve(outputFolder, file.replace(/(\.[^.]*)?$/, '_res.json'));
    fs.stat(fromPath, (e, stat) => {
      if (!e && stat.isFile()) enhance(fromPath, toPath, iniPath, resPath);
    });
  });
});
```

***

## Running the batch

```bash
mkdir in out

# Copy test images to ./in/

export UV_THREADPOOL_SIZE=16
node testbatch.js
```

Each line of output is either `OK <ms>: <path>` (success) or `ERR <code>: <path>` (failure).

***

## Scaling to multiple GPUs

To use GPU acceleration, set `nGPUs` to the number of available NVIDIA GPUs. The pool is then limited to that count — one worker per GPU:

```js
const nGPUs = 2; // use 2 GPUs
```

One worker per GPU avoids VRAM contention. Running more workers than GPUs does not improve throughput and may cause OOM errors on the GPU.

***

## Express server integration example

This pattern integrates the pool into an Express upload endpoint:

```js
const express = require('express');
const multer = require('multer');
const { StaticPool } = require('node-worker-threads-pool');
const path = require('path');
const fs = require('fs');

const app = express();
const upload = multer({ dest: '/tmp/uploads/' });

const pool = new StaticPool({
  size: Number(process.env.UV_THREADPOOL_SIZE) || 4,
  task: './worker.js',
  workerData: process.env.VIESUS_GUID
});

app.post('/enhance', upload.single('image'), async (req, res) => {
  const fromPath = req.file.path;
  const toPath = fromPath + '_enhanced.jpg';
  const iniPath = path.resolve('./viesusini.json');
  const resPath = fromPath + '_result.json';

  try {
    const ms = await pool.exec({ fromPath, toPath, iniPath, resPath });
    if (ms < 0) return res.status(500).json({ error: ms });
    res.sendFile(toPath, () => {
      fs.unlink(fromPath, () => {});
      fs.unlink(toPath, () => {});
      fs.unlink(resPath, () => {});
    });
  } catch (err) {
    res.status(500).json({ error: err.message });
  }
});

app.listen(3000);
```

Pass the GUID as an environment variable — don't hardcode it:

```bash
VIESUS_GUID="your-guid" UV_THREADPOOL_SIZE=8 node server.js
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.viesus.com/reference/node.js-module/getting-started.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
