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.
Parameter | Type | Required |
---|---|---|
language | string | ✓ |
code | string | ✓ |
input | string | - |
timeout | number (ms) | - |
{ "message": "All workers completed", "jobId": "hill", "results": [ { "input": "5 1 2 3 4 5", "expected_output": "15", "result": "15", "correct": true, "timeout": "2" } ] }
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);
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" '