-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
78 lines (67 loc) · 2.12 KB
/
server.js
File metadata and controls
78 lines (67 loc) · 2.12 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Social Card Generator - Tutorial Example
*
* A simple Express server for generating social media cards using the APIVerve API.
* https://apiverve.com/marketplace/htmltoimage
*/
const express = require('express');
const path = require('path');
// ============================================
// CONFIGURATION - Add your API key here
// Get a free key at: https://dashboard.apiverve.com
// ============================================
const API_KEY = process.env.API_KEY || 'your-api-key-here';
const API_URL = 'https://api.apiverve.com/v1/htmltoimage';
const PORT = process.env.PORT || 3000;
const app = express();
app.use(express.json({ limit: '5mb' }));
app.use(express.static('public'));
// API endpoint to generate social card image
app.post('/api/generate', async (req, res) => {
const { html, width = 1200, height = 630, format = 'png' } = req.body;
if (!html) {
return res.status(400).json({ error: 'HTML content is required' });
}
if (API_KEY === 'your-api-key-here') {
return res.status(500).json({
error: 'API key not configured. Set API_KEY environment variable or edit server.js'
});
}
try {
const response = await fetch(API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': API_KEY
},
body: JSON.stringify({
html,
width,
height,
format
})
});
const result = await response.json();
if (result.status === 'ok') {
res.json({
success: true,
imageUrl: result.data.downloadURL,
width,
height,
format
});
} else {
res.status(400).json({ error: result.error || 'Generation failed' });
}
} catch (err) {
console.error('API Error:', err);
res.status(500).json({ error: 'Failed to generate image' });
}
});
// Serve the frontend
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
app.listen(PORT, () => {
console.log(`\n Social Card Generator running at http://localhost:${PORT}\n`);
});