0% found this document useful (0 votes)
55 views10 pages

NoteYard - Cloud Secured

The document discusses Compilex, a Node.js library for building online code compilers and interpreters. It provides methods to compile code in various languages like C, C++, Java, Python, C#, and VB and get the output. The document explains how to set up compilers for different languages and use the Compilex API methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views10 pages

NoteYard - Cloud Secured

The document discusses Compilex, a Node.js library for building online code compilers and interpreters. It provides methods to compile code in various languages like C, C++, Java, Python, C#, and VB and get the output. The document explains how to set up compilers for different languages and use the Compilex API methods.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

8/12/23, 4:04 PM NoteYard - Cloud Secured

Online Compiler in Node.JS


Sat Mar 05 2022

Compilex is a node.js library which is used to build


online code compiler/interpreter websites and
webservices.
You can compile and respond back outputs for all the languages that can be compiled by the
server.

Some of the online code compiling/judging websites are

codepad
ideone
HackerRank
CodeChef

Using compilex , you can built sites and services like the above said examples.

compilex is an experimental package , however , if you want to build your site quickly , you
can prefer to use the API of the above said services. Node.js Wrapper modules for those API
are available. You can try HackerRank SDK and HackerEarth SDK.

Typical scenarios in which you have to go with wrapper modules instead of compilex are

If you don't want to have the pain of configuring compilers on your server.
If you don't have the access to the VM on which your site is hosted
If you feel tired of configuring compilex over SSH.

Work Flow
1. Get the program as input from the client as a request
2. Use compilex modules to compile the program
3. Get the output and errors in json and string formats
localhost:3000/blogpost/6223668952036d4480e861ed 1/10
8/12/23, 4:04 PM NoteYard - Cloud Secured

4. Respond the output to the client

Setting Up Compilers
Inorder to compile any programming language , you need to first have the compiler for that
programming language in the server machine.

C and C++
1. Installation :You need GCC compiler that can compile programs from your cmd/terminal

Windows - You can get MinGw .


Linux - Most of the linux versions are installed with gcc by default. If you haven't got ,
Take a look at Installing GCC .

1. Testing the Environment :After installing , set your environment variables for accessing
the GCC command lines from any directory

Windows - create a c file in a directory , execute


g++ filename.c -o output.exe
output.exe
then you will get the output of the program
Linux - create a c file in a directory , execute
gcc filename.c -o output.out
./output.out
then you will get the output of the program

Java
1. Installion : You need JDK ( Java Development Kit ) to compile Java programs.Click here to
download JDK for various platforms.
2. Testing the Environment :After installing , set your environment variables for accessing
the javac command lines from any directory

Create a Java file named Main.java with main function


javac Main.java
java Main
then you will get the output of the program.

localhost:3000/blogpost/6223668952036d4480e861ed 2/10
8/12/23, 4:04 PM NoteYard - Cloud Secured

Python
1. Installation : You can get and install Python from here
2. Testing the Environment :After installing , set your environment variables for accessing
python command lines from any directory

Create a python file hello.py and execute


python hello.py
then you will get the output of the program.

C# and VB
1. Installation : You can have the idea of accessing C# compiler from here . This step also
adds VB compiler to the scope automatically as csc and vbc are located in the same
directory
2. Testing the Environment :After installing , set your environment variables for accessing
C# and VB command lines from any directory

Create a C# or VB file Hello.cs or Hello.vb and execute


csc Hello.cs
Hello.exe
or
vbc Hello.vb
Hello.exe
then you will get the output of the program.

Documentation

1)Require compilex
var compiler = require('compilex');
var options = {stats : true}; //prints stats on console
compiler.init(options);

init() creates a folder named temp in your project directory which is used for storage purpose.
Before using other methods , make sure to call init() method.

2)C and C++

localhost:3000/blogpost/6223668952036d4480e861ed 3/10
8/12/23, 4:04 PM NoteYard - Cloud Secured

//if windows
var envData = { OS : "windows" , cmd : "g++"}; // (uses g++ command to compile )
//else
var envData = { OS : "linux" , cmd : "gcc" }; // ( uses gcc command to compile )
compiler.compileCPP(envData , code , function (data) {
res.send(data);
//data.error = error message
//data.output = output value
});

//res is the response object

3)C and C++ with inputs


//if windows
var envData = { OS : "windows" , cmd : "g++"}; // (uses g++ command to compile )
//else
var envData = { OS : "linux" , cmd : "gcc" }; // ( uses gcc command to compile )
compiler.compileCPPWithInput(envData , code , input , function (data) {
res.send(data);
});

4)Java
//if windows
var envData = { OS : "windows"};
//else
var envData = { OS : "linux" }; // (Support for Linux in Next version)
compiler.compileJava( envData , code , function(data){
res.send(data);
});

5)Java with inputs


//if windows
var envData = { OS : "windows"};
//else
var envData = { OS : "linux" }; // (Support for Linux in Next version)
compiler.compileJavaWithInput( envData , code , input , function(data){
res.send(data);
});

localhost:3000/blogpost/6223668952036d4480e861ed 4/10
8/12/23, 4:04 PM NoteYard - Cloud Secured

6)Python
//if windows
var envData = { OS : "windows"};
//else
var envData = { OS : "linux" };
compiler.compilePython( envData , code , function(data){
res.send(data);
});

7)Python with inputs


//if windows
var envData = { OS : "windows"};
//else
var envData = { OS : "linux" };
compiler.compilePythonWithInput( envData , code , input , function(data){
res.send(data);
});

8)C#
var envData = { OS : "windows"};
//mono modules for linux is not included till now
compiler.compileCS( envData , code , function(data){
res.send(data);
});

9)C# with inputs

var envData = { OS : "windows"};


//mono modules for linux is not included till now
compiler.compileCSWithInput( envData , code , input , function(data){
res.send(data);
});

10)Visual Basic

localhost:3000/blogpost/6223668952036d4480e861ed 5/10
8/12/23, 4:04 PM NoteYard - Cloud Secured

var envData = { OS : "windows"};


compiler.compileVB( envData , code , function(data){
res.send(data);
});

11)Visual Basic with inputs

var envData = { OS : "windows"};


compiler.compileVBWithInput( envData , code , input , function(data){
res.send(data);
});

12)Memory Management
All the temporary files ( source code and executables ) are created in your temp directory.
flush and flushSync helps you to free the memory by deleting the temporary files.

compiler.flush(function(){
console.log('All temporary files flushed !');
});

Synchronous version of flush

compiler.flushSync();

13)Statistical Data
Getting statistics about your compilex server has been taken care. fullStat returns json data
about your server.

compiler.fullStat(function(data){
res.send(data);
});

1)options : (windows only c/c++ only)


timeout: number of milliseconds to wait before killing the compiled program

localhost:3000/blogpost/6223668952036d4480e861ed 6/10
8/12/23, 4:04 PM NoteYard - Cloud Secured

//compile and execute the file and kill it after 1 second if it still running
var envData = { OS : "linux" , cmd : "gcc" ,options: {timeout:1000 } };
compiler.compileCPP(envData , code , function (data) {
res.send(data);
//data.error = error message
//data.output = output value
});

Examples :

Index.html -

<html>
<head>
<title>Online IDE</title>
</head>
<body>
<center>
<h1>Online IDE</h1>
<form id="myform" name="myform" method="post" action="compilecode">
<h3>Code</h3>
<textarea rows="13" cols="100" id="code" name="code"></textarea>
<br />
<h3>Input</h3>
<textarea rows="10" cols="100" id="input" name="input"></textarea>
<br />
Language :
<select name="lang">
<option value="C">C</option>
<option value="Java">Java</option>
<option value="Python">Python</option>
</select>
Compile With Input :
<input type="radio" name="inputRadio" id="inputRadio" value="true" />yes
<input type="radio" name="inputRadio" id="inputRadio" value="false" />No
<br />
<input type="submit" value="submit" name="submit" />
</form>

localhost:3000/blogpost/6223668952036d4480e861ed 7/10
8/12/23, 4:04 PM NoteYard - Cloud Secured

</center>
</body>
</html>

index.js

var express = require("express");


var path = require("path");
var bodyParser = require("body-parser");
var compiler = require("compilex");

var app = express();


app.use(bodyParser());

var option = { stats: true };


compiler.init(option);
app.get("/", function (req, res) {
res.sendfile(__dirname + "/index.html");
});

app.post("/compilecode", function (req, res) {


var code = req.body.code;
var input = req.body.input;
var inputRadio = req.body.inputRadio;
var lang = req.body.lang;
if (lang === "C" || lang === "C++") {
if (inputRadio === "true") {
var envData = { OS: "windows", cmd: "g++", options: { timeout: 10000 } };
compiler.compileCPPWithInput(envData, code, input, function (data) {
if (data.error) {
res.send(data.error);
} else {
res.send(data.output);
}
});
} else {
var envData = { OS: "windows", cmd: "g++", options: { timeout: 10000 } };
compiler.compileCPP(envData, code, function (data) {
res.send(data);

localhost:3000/blogpost/6223668952036d4480e861ed 8/10
8/12/23, 4:04 PM NoteYard - Cloud Secured

//data.error = error message


//data.output = output value
});
}
}
if (lang === "Python") {
if (inputRadio === "true") {
var envData = { OS: "windows" };
compiler.compilePythonWithInput(envData, code, input, function (data) {
res.send(data);
});
} else {
var envData = { OS: "windows" };
compiler.compilePython(envData, code, function (data) {
res.send(data);
});
}
}
});

app.get("/fullStat", function (req, res) {


compiler.fullStat(function (data) {
res.send(data);
});
});

app.listen(8080);

compiler.flush(function () {
console.log("All temporary files flushed !");
});

Read More on : Compilex

Watch Full Video :


localhost:3000/blogpost/6223668952036d4480e861ed 9/10
8/12/23, 4:04 PM NoteYard - Cloud Secured

Online Compiler in Node.JS( my own code compiler for website)

1 Comment

Add a comment...

EasyBucket.Pk
nicely donee
Like · Reply · 26w

Facebook Comments plugin

localhost:3000/blogpost/6223668952036d4480e861ed 10/10

You might also like