const { create, all } = require('mathjs') const http = require('http') const math = create(all) function formatResult(val) { if (val === undefined || val === null) return null if (typeof val === 'function') return null if (typeof val === 'number' || typeof val === 'string' || typeof val === 'boolean') return String(val) try { return math.format(val) } catch(e) { return null } } const server = http.createServer((req, res) => { if (req.method === 'POST' && req.url === '/calculate') { let body = '' req.on('data', chunk => body += chunk) req.on('end', () => { try { const { expr } = JSON.parse(body) const lines = expr.split('\n').filter(l => l.trim()) const results = math.evaluate(lines) const arr = Array.isArray(results) ? results : [results] const output = lines.map((line, i) => { const r = formatResult(arr[i]) return r !== null ? line + ' = ' + r : null }).filter(Boolean).join('\n') res.writeHead(200) res.end(JSON.stringify({ result: output || '' })) } catch(e) { res.writeHead(400) res.end(JSON.stringify({ error: e.message })) } }) return } res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' }) res.end(getHtml()) }) function getHtml() { return `