Node.js Backend
This tutorial shows how to create a Node.js project with a JavaScript backend and npm dependencies.
Step 1: Create the Project
mkdir -p plugins/ZyveraWeb/site/my-api/public
Step 2: Create the Backend
plugins/ZyveraWeb/site/my-api/server.js:
function handleRequest(request) {
// Enable CORS
var cors = { 'Access-Control-Allow-Origin': '*' };
// GET /api/time — Return current server time
if (request.path === '/api/time' && request.method === 'GET') {
return {
status: 200,
headers: Object.assign(cors, { 'Content-Type': 'application/json' }),
body: JSON.stringify({
time: new Date().toISOString(),
timestamp: Date.now()
})
};
}
// POST /api/echo — Echo back the request body
if (request.path === '/api/echo' && request.method === 'POST') {
return {
status: 200,
headers: Object.assign(cors, { 'Content-Type': 'application/json' }),
body: JSON.stringify({
method: request.method,
path: request.path,
body: request.body
})
};
}
// GET /api/players — Proxy to ZyveraWeb's built-in players API
if (request.path === '/api/players' && request.method === 'GET') {
return {
status: 200,
headers: Object.assign(cors, { 'Content-Type': 'text/plain' }),
body: request.headers['host'] || 'unknown'
};
}
return null;
}
Step 3: Add npm Dependencies (Optional)
plugins/ZyveraWeb/site/my-api/package.json:
{
"dependencies": {
"lodash": "^4.17.21",
"moment": "^2.29.4"
}
}
Step 4: Create a Frontend (Optional)
plugins/ZyveraWeb/site/my-api/public/index.html:
<!DOCTYPE html>
<html>
<head>
<title>My API</title>
</head>
<body>
<h1>My API Server</h1>
<button onclick="fetchTime()">Get Time</button>
<pre id="output"></pre>
<script>
function fetchTime() {
fetch('/api/time')
.then(r => r.json())
.then(data => {
document.getElementById('output').textContent =
JSON.stringify(data, null, 2);
});
}
</script>
</body>
</html>
Step 5: Configure the Site
sites:
- domain: "api.myserver.com"
dir: "site/my-api"
Step 6: Restart and Test
/zyveraweb server restart
The plugin will auto-install npm packages. Monitor the console for progress.
Step 7: Verify
curl http://your-server:8080/api/time
Expected response:
{"time": "<current-iso-timestamp>", "timestamp": <unix-ms>}
No comments to display
No comments to display