const http = require('http'); const mjAPI = require('mathjax-node'); mjAPI.config({ MathJax: { } }); mjAPI.start(); const server = http.createServer((req, res) => { if (req.method === 'POST') { let body = ''; req.on('data', chunk => { body += chunk.toString(); }); req.on('end', () => { const requestData = JSON.parse(body); const tex = requestData.tex; mjAPI.typeset({ math: tex, format: "TeX", // or "inline-TeX", "MathML" svg: true, // or html:true }, function (data) { if (!data.errors) { res.writeHead(200, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ html: data.svg })); } else { res.writeHead(400, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: data.errors })); } }); }); } else { res.writeHead(405, { 'Content-Type': 'application/json' }); res.end(JSON.stringify({ error: "Method not allowed" })); } }); const port = process.env.PORT || 3000; server.listen(port, () => { console.log('MathJax server running on port ' + port + '...'); });