1

I want to try and build something like codeacedemy. Where a client can write code in diffrent languages (Java, Phython ...) for a given question on a website and run tests against there answer when you submit the code.

My guess would be that the code has to be validated in the backend. Im useing node.js. So how would you validate the code for example java with Junit in a node.js and then send back a response to the client.

Jakob Wolf
  • 36
  • 6
  • I also found a similuar question her: https://stackoverflow.com/questions/39855963/online-java-coding-test-compile-submitted-java-code-in-nodejs – Jakob Wolf May 03 '22 at 05:05

1 Answers1

0

I think you need this: eval()

let stringToEval = "let num = 23; console.log(num);"

eval(stringToEval)
Hidenplus
  • 49
  • 4
  • Note that this only works for Javascript (he asked for different languages) , besides using eval with user input is not safe. – Kays Kadhi May 03 '22 at 09:39
  • @KaysKadhi how would you go about the problem? Im still searching tbh. I think the best solution would be to install a compiler for each language and then pass the code from the frontend to the backend and back – Jakob Wolf May 03 '22 at 14:24
  • Yes I think the best way would be to install compilers/interpreters in the server like you said, just don't forget to execute it as an unprivileged user etc (since you can't trust user input code executing on your server ) – Kays Kadhi May 03 '22 at 14:32
  • Thanks. How would you install the a compiler in lets say node.js and how would you communicate between them? – Jakob Wolf May 03 '22 at 14:52
  • I tought you need to create a socket for each server and later pass the string and make the interpreter/compiler validate the code. – Hidenplus May 03 '22 at 15:20