node-web
English
Node.js 20 web service sandbox environment, suited for rapid development and deployment with Express, Fastify, Koa, and similar frameworks.
Environment
| Category | Details |
|---|---|
| Base image | python:3.12-slim (Node.js installed for the web runtime) |
| Node.js | Node.js 20.x + npm |
| Global tools | yarn, nodemon, pm2 |
| System tools | curl, wget, git, jq |
| Resources | 1 CPU / 2048 MB memory |
| Exposed port | 3000 |
Installation
Install from a local path:
abx template install ./node-web --registry-type local
Install from GitHub:
abx template install alibaba-agent-sandbox/awesome-templates//node-web
Usage
Create a sandbox instance:
abx create --template node-web
Initialize an Express project inside the sandbox and start it:
abx exec <sandbox-id> -- bash -c "
cd /workspace && \
npm init -y && \
npm install express && \
cat > index.js << 'EOF'
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
app.get('/', (req, res) => {
res.json({ message: 'Hello from Agent Sandbox!' });
});
app.listen(port, () => {
console.log('Server running on port ' + port);
});
EOF
node index.js
"
Using the Python SDK:
from agent_sandbox import Sandbox
sandbox = Sandbox.create_sync(template="node-web")
# Write the server code
sandbox.files.write("/workspace/index.js", """
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({status: 'ok'}));
});
server.listen(3000);
""")
# Start the service
sandbox.commands.run("node /workspace/index.js &")
# Test access
result = sandbox.commands.run("curl -s http://localhost:3000")
print(result.stdout)
Configuration
Environment Variables
| Variable | Description | Default |
|---|---|---|
NODE_ENV |
Node.js runtime environment | production |
PORT |
Web service listen port | 3000 |
Managing Processes with pm2
pm2 is preinstalled and available for process management:
# Start the service
pm2 start index.js --name my-app
# Check status
pm2 status
# View logs
pm2 logs my-app
Development Mode with nodemon
During development, use nodemon for hot reload:
NODE_ENV=development nodemon index.js
Notes
- Port mapping: Port 3000 is exposed by default; to use other ports, specify them via configuration when creating the sandbox.
- Production environment:
NODE_ENVdefaults toproduction; during development, set it todevelopmentfor detailed error messages. - Package managers: Both npm and yarn are installed — choose based on project needs.
- Process management: In production, using pm2 to manage Node.js processes is recommended for stable service operation.
中文
Node.js 20 Web 服务沙箱环境,适用于 Express、Fastify、Koa 等框架的快速开发与部署。
环境说明
| 类别 | 内容 |
|---|---|
| 基础镜像 | python:3.12-slim(镜像内安装 Node.js,作为 Web 运行时) |
| Node.js | Node.js 20.x + npm |
| 全局工具 | yarn、nodemon、pm2 |
| 系统工具 | curl、wget、git、jq |
| 资源配置 | 1 CPU / 2048 MB 内存 |
| 暴露端口 | 3000 |
安装方式
从本地安装:
abx template install ./node-web --registry-type local
从 GitHub 安装:
abx template install alibaba-agent-sandbox/awesome-templates//node-web
使用示例
创建沙箱实例:
abx create --template node-web
在沙箱中初始化 Express 项目并启动:
abx exec <sandbox-id> -- bash -c "
cd /workspace && \
npm init -y && \
npm install express && \
cat > index.js << 'EOF'
const express = require('express');
const app = express();
const port = process.env.PORT || 3000;
app.get('/', (req, res) => {
res.json({ message: 'Hello from Agent Sandbox!' });
});
app.listen(port, () => {
console.log('Server running on port ' + port);
});
EOF
node index.js
"
使用 Python SDK:
from agent_sandbox import Sandbox
sandbox = Sandbox.create_sync(template="node-web")
# 写入服务器代码
sandbox.files.write("/workspace/index.js", """
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'application/json'});
res.end(JSON.stringify({status: 'ok'}));
});
server.listen(3000);
""")
# 启动服务
sandbox.commands.run("node /workspace/index.js &")
# 测试访问
result = sandbox.commands.run("curl -s http://localhost:3000")
print(result.stdout)
配置说明
环境变量
| 变量名 | 说明 | 默认值 |
|---|---|---|
NODE_ENV |
Node.js 运行环境 | production |
PORT |
Web 服务监听端口 | 3000 |
使用 pm2 管理进程
pm2 已预装,可用于进程管理:
# 启动服务
pm2 start index.js --name my-app
# 查看状态
pm2 status
# 查看日志
pm2 logs my-app
使用 nodemon 开发模式
开发阶段可使用 nodemon 实现热重载:
NODE_ENV=development nodemon index.js
注意事项
- 端口映射:默认暴露 3000 端口,如需其他端口,请在创建沙箱时通过配置指定。
- 生产环境:
NODE_ENV默认为production,开发时可设为development以获取详细错误信息。 - 包管理器:npm 和 yarn 均已安装,根据项目需要选择使用。
- 进程管理:生产环境建议使用 pm2 管理 Node.js 进程,确保服务稳定运行。
Deploy and local e2e
Install and register this template:
abx template install alibaba-agent-sandbox/awesome-templates//node-web
From this directory (build, push, and register):
abx deploy
From the repository root, with agent-sandbox-server and httpx installed:
python3 scripts/run_local_e2e.py
That starts python3 commands.py in this directory and runs e2e_test.py.