Files
marqdo-ext-web/examples/research-workbench-app/server.js
cflmy 7a2c9280c0 feat: WEB-04 domain composition cases and production delivery trial
Add three composition cases driven by the same generic web Tool:
- examples/doc-site (documentation site),
- examples/research-workspace (existing section 8 workbench prototype),
- examples/quantum-editor (finite scenario/interaction design).

Add a real production delivery trial in examples/research-workbench-app
(Express + JSON persistence + static UI): validation, search, status
transitions, linked statistics, JSON errors, real file persistence; node:test
API acceptance and a Playwright browser E2E. The production app is not a runtime
dependency of the web Tool; prototype PASS is kept separate from production
PASS. docs/WEB-04-delivery.md records the design-source to implementation
mapping, intentional non-goals and task splits.

scripts/domain-acceptance.sh is the explicit domain entry: installs the built
Tool, runs check/preview/handoff for the three cases, then installs/tests the
production app incl. browser E2E, and removes the Tool. Evidence records the
real CLI/Design/Preview identities and local PASS results.

Pin the domain submodules to the merged Design (WEB-01/03) and Preview (WEB-02)
candidates; activation and root gitlink belong to WEB-05.

Refs cflmy/marqdo-ext-web#2, cflmy/marqdo#6
2026-11-27 09:02:27 +08:00

100 lines
3.8 KiB
JavaScript

// Production delivery trial for the Marqdo research workbench design.
// This is a real application (Express + JSON persistence), NOT the web Tool:
// the design/Preview prototype never runs as a dependency of this server.
import express from "express";
import { readFileSync, writeFileSync, existsSync } from "node:fs";
import { dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
const here = dirname(fileURLToPath(import.meta.url));
export function createApp({ dataFile }) {
const app = express();
app.use(express.json({ limit: "64kb" }));
app.use(express.static(join(here, "public")));
const load = () => {
if (!existsSync(dataFile)) return { nextId: 1, tasks: [] };
return JSON.parse(readFileSync(dataFile, "utf8"));
};
const save = (state) => writeFileSync(dataFile, JSON.stringify(state, null, 2));
const fail = (response, status, code, message) =>
response.status(status).json({ error: { code, message } });
app.get("/api/tasks", (request, response) => {
const state = load();
const query = String(request.query.q ?? "").trim().toLowerCase();
const status = String(request.query.status ?? "").trim();
const tasks = state.tasks.filter((task) => {
const matchesQuery = !query || task.title.toLowerCase().includes(query);
const matchesStatus = !status || task.status === status;
return matchesQuery && matchesStatus;
});
response.json({ tasks });
});
app.post("/api/tasks", (request, response) => {
const title = typeof request.body?.title === "string" ? request.body.title.trim() : "";
const priority = request.body?.priority ?? "normal";
if (!title) return fail(response, 400, "task.title_required", "任务标题不能为空");
if (title.length > 120) return fail(response, 400, "task.title_too_long", "任务标题不能超过 120 字符");
if (!["low", "normal", "high"].includes(priority)) {
return fail(response, 400, "task.priority_invalid", "优先级必须是 low/normal/high");
}
const state = load();
const task = {
id: state.nextId++,
title,
priority,
status: "open",
createdAt: new Date().toISOString(),
};
state.tasks.push(task);
save(state);
response.status(201).json({ task });
});
app.patch("/api/tasks/:id/status", (request, response) => {
const id = Number(request.params.id);
const status = request.body?.status;
if (!Number.isInteger(id) || id < 1) return fail(response, 400, "task.id_invalid", "任务 id 非法");
if (!["open", "doing", "done"].includes(status)) {
return fail(response, 400, "task.status_invalid", "状态必须是 open/doing/done");
}
const state = load();
const task = state.tasks.find((item) => item.id === id);
if (!task) return fail(response, 404, "task.not_found", "任务不存在");
task.status = status;
save(state);
response.json({ task });
});
app.get("/api/stats", (request, response) => {
const state = load();
const total = state.tasks.length;
const byStatus = { open: 0, doing: 0, done: 0 };
const byPriority = { low: 0, normal: 0, high: 0 };
for (const task of state.tasks) {
byStatus[task.status] += 1;
byPriority[task.priority] += 1;
}
response.json({ total, byStatus, byPriority });
});
app.use((request, response) =>
fail(response, 404, "app.not_found", `unknown route ${request.method} ${request.path}`),
);
return app;
}
const isMain = process.argv[1] && fileURLToPath(import.meta.url) === process.argv[1];
if (isMain) {
const port = Number(process.env.PORT ?? 4173);
const dataFile = process.env.WORKBENCH_DATA ?? join(here, "workbench-data.json");
createApp({ dataFile }).listen(port, "127.0.0.1", () => {
console.log(`research-workbench listening on http://127.0.0.1:${port} (data: ${dataFile})`);
});
}