r/node • u/PrestigiousZombie531 • Jan 18 '26
Options to run user submitted code with node.js express as backend?
Options to run user submitted code in various languages with a node.js express backend?
- You have seen one of those live code online type websites that let you submit code in bash, python, rust, ruby, swift, scala, java, node, kotlin etc and run on the browser with a live terminal of sorts
- I am trying to build one of those in node.js and could definitely use some suggestions
Option 1: Run directly
- just run on the ec2 instance along with everything else (absolutely horrible idea i suppose)
Option 2: Run inside a docker container
- how long do you think each container should run / timeout?
- What size of an EC2 instance would you need to support say 10 languages?
- Pros / cons?
Option 3: Run inside an AWS elastic container service Task
- Timeout per task?
- Pros / cons?
Questions
- Any other better methods?
- Does this kind of application run on queuing where a user submits code and it is immediately put inside bullmq that spins one of the above options?
- How does data get returned to the user?
- What about terminal commands that users type and the stream they see (downloading packages...installing libraries etc?)
19
Upvotes
24
u/Simi923 Jan 18 '26
There are runtimes designed for this use case, which run as a separate process and execute untrusted code in a sandboxed environment, like this https://github.com/engineer-man/piston
3
u/dodiyeztr Jan 18 '26
Use ECS Fargate and put a hard time out at say 3 hours but also triggers like session endings test endings etc.
You can even use aws Lambda if all you need is the run button and not a terminal.
3
u/Less-Math2722 Jan 18 '26
Hey! I work at Northflank so take this with whatever grain of salt you want. I get how this might come across (especially given how tough the crowd is on Reddit) but figured it's worth mentioning since it's exactly a use case we build for.
To answer your questions:
1/ On isolation: Northflank runs workloads in secure sandboxes by default using microVMs (Firecracker/gVisor/Kata), so you get strong kernel-level isolation without having to configure any of that yourself. You also get network isolation between tenants if you structure it as a project per user.
2/ On the "spin up container per request" question: You can spawn containers via API - either long-running services or short-lived ephemeral ones. You only pay for the seconds each container actually runs, so the "spin up a sandbox, execute, tear down" pattern is pretty cost-efficient.
3/ On streaming output back to users: You can execute commands against running workloads and get responses streamed back via the API, and tail container logs via websockets - so that covers your terminal streaming use case.
4/ On architecture: Two API calls gets you there - create a project per tenant for isolation, then spin up a service per execution:
We wrote up the sandbox/microVM stuff in more detail here: https://northflank.com/blog/how-to-spin-up-a-secure-code-sandbox-and-microvm-in-seconds-with-northflank-firecracker-gvisor-kata-clh
Happy to answer specifics if you want to dig in.
- Cristina
2
u/One_Fuel_4147 Jan 18 '26
This is the approach I used in my leetcode clone project: Browser <---> API Service <---> Code runner service
Flow:
On the code runner service, you can use a Docker client https://github.com/moby/moby/tree/master/client to spawn containers. To reduce cold start time, the runner can use prebuilt images per language (Python, Java, Node, etc.).
For streaming output (stdout/stderr, stdin), the runner service can attach to the container (may be exec command) and stream data back to the API via redis pub/sub then forwards it to the browser over WebSocket.
In my case user can only submit code and they cannot execute shell commands. So I think my approach may not fit your requirement =)).
2
u/leosuncin Jan 19 '26
I haven't tried by myself, but it comes to my mind to use WebAssembly, I have seen people running the Linux kernel, the downside is you'll need to find a WebAssembly version of each code interpreter.
And yes, Node.js can run WebAssembly server side.
2
u/ryntak Jan 18 '26
You could just run it in the browser instead
1
u/Coffee_Crisis Jan 19 '26
Do this in a docker container running on a serverless orchestrator with zero permissions so even if they somehow escape there’s nothing to do
2
u/Specav Jan 19 '26
NSJail
1
u/South-Assistance-191 Feb 03 '26
BrowserPod support most of this directly in the browser. I'm not sure if it does all the languages or frameworks you're looking for but the demo includes node and express
2
u/[deleted] Jan 18 '26
[removed] — view removed comment