math-tasks/bs-config.cjs

46 lines
1.3 KiB
JavaScript

const fs = require('fs');
const path = require('path');
module.exports = {
server: {
baseDir: "output",
routes: {
"/assets": "assets"
}
},
files: [
"output/index.html",
"output/html/**/*.html",
"assets/**/*"
],
port: 3300,
open: false,
notify: false,
ui: false,
middleware: [
{
route: "/api/save-editor",
handle: function (req, res, next) {
if (req.method !== 'POST') return next();
let body = '';
req.on('data', chunk => body += chunk);
req.on('end', () => {
try {
const data = JSON.parse(body);
const name = path.basename(data.file || 'unknown', '.html');
const dir = path.join(__dirname, 'output', 'editor-saves');
fs.mkdirSync(dir, { recursive: true });
const savePath = path.join(dir, name + '.json');
fs.writeFileSync(savePath, JSON.stringify(data, null, 2));
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ ok: true, path: savePath }));
} catch (e) {
res.writeHead(400, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ error: e.message }));
}
});
}
}
]
};