mathjax-server/server.js
Kumi d861af19e2
feat: initialize MathJax server with TeX to SVG conversion
Add initial setup for a MathJax server to convert TeX input to SVG output. The server responds to POST requests containing TeX math by converting it into an SVG representation. This setup includes:
- package.json with required dependencies and start script
- package-lock.json to lock dependencies
- .gitignore to exclude node_modules
- server.js to handle HTTP requests and MathJax configuration

This setup provides a foundation for further enhancements and integrations related to TeX-to-SVG conversion tasks.
2024-08-03 14:24:21 +02:00

46 lines
No EOL
1.1 KiB
JavaScript

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 + '...');
});