Running Applications
ជំពូកនេះមិនទាន់ត្រូវបានបកប្រែជាភាសាខ្មែរនៅឡើយទេ — សូមអានជាភាសាអង់គ្លេសខាងក្រោម។
Table of Contents
- Create a small project
- Install dependencies
- Write the server
- Run it, bound to all interfaces
- Publish the port
- Access it from your host
- Stop and restart
- Beginner Pitfalls
1. Create a small project
Inside the sandbox (sbx run --name sbx-crash-course if you're not already
attached), create a project in your workspace so the files are visible from
your host too:
mkdir -p ~/sbx-crash-course/app && cd ~/sbx-crash-course/app
npm init -y
2. Install dependencies
npm install express
This downloads and installs into node_modules inside the sandbox, exactly
like it would on your host — except it's isolated here, and gone the moment
you remove the sandbox.
3. Write the server
cat > index.js << 'EOF'
const express = require("express");
const app = express();
const port = 3000;
app.get("/", (req, res) => {
res.json({ message: "Hello from inside the sandbox!", pid: process.pid });
});
app.listen(port, "0.0.0.0", () => {
console.log(`Listening on http://0.0.0.0:${port}`);
});
EOF
4. Run it, bound to all interfaces
node index.js
Note: the
"0.0.0.0"inapp.listenmatters. A published port only reaches a service that's listening on all interfaces inside the sandbox — a server bound to127.0.0.1(the sandbox's own loopback) is unreachable from the outside no matter what you publish.
Leave this running and open a second terminal on your host for the next steps.
5. Publish the port
From your host, publish sandbox port 3000 to a host port:
sbx ports sbx-crash-course --publish 3000:3000
Check it took effect:
sbx ports sbx-crash-course
SANDBOX PORT HOST ADDRESS
3000 127.0.0.1:3000
sbx ls shows the same mapping alongside the sandbox's status.
6. Access it from your host
curl http://localhost:3000
{"message":"Hello from inside the sandbox!","pid":1234}
Or just open http://localhost:3000 in your browser. The request left your
host, crossed into the sandbox through the published port, and hit the
Express server running there — the same round trip you'd get from a
container, just with a whole isolated Linux VM behind it instead.
7. Stop and restart
Back in the sandbox's terminal, Ctrl-C stops the server. Restart it the
same way:
node index.js
To run it in the background instead of holding a terminal open, use sbx exec from your host with -d (detached):
sbx exec -d sbx-crash-course bash -c "cd ~/sbx-crash-course/app && node index.js"
Leave it running in the background — the mini project in Part 5 builds alongside it, and Part 6 replaces it with a fuller backend.
8. Beginner Pitfalls
- Forgetting
"0.0.0.0"and leaving the default (which for most frameworks is127.0.0.1orlocalhost) is the single most common reason "it works inside the sandbox but I can't reach it from my browser." sbx runonly applies--publishwhen it creates a sandbox. Sincesbx-crash-coursealready existed, we usedsbx portsto publish afterward — remember that distinction.- An explicit host port (like
3000:3000above) has to be free on your host. If it's taken, publish without a host port (--publish 3000) and let the OS assign one, then check it withsbx ports.
ទិញកាហ្វេជូនខ្ញុំ
ចូលចិត្តមេរៀននេះមែនទេ? សូមពិចារណាទិញកាហ្វេជូនខ្ញុំមួយកែវ ដើម្បីគាំទ្រការបង្កើតមាតិកាឥតគិតថ្លៃបន្ថែមទៀត!