JudgeLib

Documentation

Complete guides and API references for integrating JudgeLib into your applications.

Getting Started

What is JudgeLib?

libJudge is a lightweight code execution engine built with Node.js and the `child_process` module. It enables you to execute user-submitted code in a controlled environment for educational, testing, or evaluation purposes.

Note: While it isolates processes, it does not run inside a secure container (like Docker). For production-grade isolation, containerization is recommended.

Key Features:

  • Execute code in multiple languages (like Python, C++, Java)
  • Customizable timeouts and memory limits
  • Queue-based execution support (optional via Redis)
  • Real-time output streaming
  • Supports both NPM module usage and HTTP-based microservice mode

API Reference

Execution Parameters
Parameters for code execution requests
ParameterTypeRequired
languagestring
codestring
inputstring-
timeoutnumber (ms)-
Response Format
Structure of execution results
{
    "message": "All workers completed",
    "jobId": "hill",
    "results": [
        {
            "input": "5 1 2 3 4 5",
            "expected_output": "15",
            "result": "15",
            "correct": true,
            "timeout": "2"
        }
      ]
    }

Supported Languages

Python
C++
Java

Code Examples

Basic C++ Execution with File Path
import fs from 'fs';
import path from 'path';
import { judge } from 'lib-judge';

// C++ code for summing array elements
const code = `
#include<iostream>
#include<vector>
using namespace std;
int main(){
    int n;
    cin>>n;
    vector<int> v(n);
    for(int i = 0;i<n;i++){
        cin>>v[i];
    }
    int c = 0;
    for(auto it:v){
        c+=it;
    }
    cout<<c;
    return 0;
}`;

// Save to a temporary file
const tmpDir = './tmp';
if (!fs.existsSync(tmpDir)) fs.mkdirSync(tmpDir);
const tmpPath = path.join(tmpDir, `code_${Date.now()}.cpp`);
fs.writeFileSync(tmpPath, code, 'utf-8');

// Execute with judge
const result = await judge({
  codePath: tmpPath,        // path of the file
  ques_name: 'sum of array',
  input: '5 1 2 3 4 5 ### 3 1 2 3 ### 2 1 2',
  output: '15 ### 6 ### 3',
  timeout: '2',
  sizeout: '64',
});

console.log(result);
Microservice API Call
curl -X POST http://<your-api-endpoint>/run \
  -F "file=./code.cpp" \
  -F "ques_name= sum of number" \
  -F "timeout=2" \
  -F "sizeout=1" \
  -F "input=5 1 2 3 4 5 ### 3 1 2 3 ### 2 1 4..." // each input are seperated by ### \
  -F "output=15 ### 6 ### 5 .." // each output are seperated by ### \
  -F "language=cpp"
'