-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
43 lines (35 loc) · 1.16 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
const express = require('express');
const { fork } = require('child_process');
const app = express();
app.get('/draw', async (req, res) => {
try {
// Decode the base64 URL parameter
const paramsBase64 = req.query.params;
const paramsBuffer = Buffer.from(paramsBase64, 'base64');
const params = JSON.parse(paramsBuffer.toString());
// Fork a new process to handle the canvas drawing
const canvasWorker = fork('./canvasWorker.js');
canvasWorker.send(params);
canvasWorker.on('message', (message) => {
if (message.success) {
const pngBuffer = Buffer.from(message.data, 'base64');
res.setHeader('Content-Type', 'image/png');
res.end(pngBuffer);
} else {
res.status(500).send(`Error: ${message.error}`);
}
// Terminate the child process
canvasWorker.kill();
});
canvasWorker.on('error', (error) => {
res.status(500).send('Error processing request');
canvasWorker.kill();
});
} catch (error) {
res.status(400).send('Invalid parameters');
}
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});