loki169's picture
does it have backend also ur have u only used html and css and not javascript?
c2ca98a verified
Raw History Blame Contribute Delete
3.53 kB
```javascript
const express = require('express');
const multer = require('multer');
const cors = require('cors');
const fs = require('fs');
const path = require('path');
const { v4: uuidv4 } = require('uuid');
const AdmZip = require('adm-zip');
const app = express();
const upload = multer({ dest: 'uploads/' });
// Middleware
app.use(cors());
app.use(express.json());
app.use(express.static('public'));
// In-memory storage for demo purposes
const designs = {};
const guestLists = {};
const batches = {};
// API Endpoints
app.post('/api/v1/upload/design', upload.single('design'), (req, res) => {
const designId = uuidv4();
designs[designId] = {
path: req.file.path,
originalname: req.file.originalname,
mimetype: req.file.mimetype
};
res.json({ designId });
});
app.post('/api/v1/upload/guestlist', upload.single('guestList'), (req, res) => {
const guestListId = uuidv4();
guestLists[guestListId] = {
path: req.file.path,
originalname: req.file.originalname
};
res.json({ guestListId });
});
app.post('/api/v1/generate', async (req, res) => {
const { designId, guestListId, position, fontSettings } = req.body;
// Validate inputs
if (!designs[designId] || !guestLists[guestListId]) {
return res.status(400).json({ error: 'Invalid design or guest list' });
}
// Process guest list (simplified for demo)
const guests = ['Mr. & Mrs. Patel', 'Dr. Sharma Family', 'The Gupta Family'];
// Generate PDFs (simplified for demo)
const batchId = uuidv4();
const outputDir = path.join(__dirname, 'output', batchId);
fs.mkdirSync(outputDir, { recursive: true });
// Create ZIP file
const zip = new AdmZip();
guests.forEach(guest => {
const filename = `${guest.replace(/[^a-z0-9]/gi, '_')}.pdf`;
const pdfPath = path.join(outputDir, filename);
// In a real app, generate actual PDFs here
fs.writeFileSync(pdfPath, 'PDF content would go here');
zip.addLocalFile(pdfPath);
});
const zipPath = path.join(outputDir, 'invitations.zip');
zip.writeZip(zipPath);
batches[batchId] = { zipPath };
res.json({ batchId });
});
app.get('/api/v1/download/:batchId', (req, res) => {
const batch = batches[req.params.batchId];
if (!batch) {
return res.status(404).send('Batch not found');
}
res.download(batch.zipPath, 'wedding_invitations.zip', (err) => {
if (err) {
console.error('Download failed:', err);
res.status(500).send('Download failed');
}
// Clean up files
try {
fs.rmSync(path.dirname(batch.zipPath), { recursive: true });
delete batches[req.params.batchId];
} catch (cleanupErr) {
console.error('Cleanup failed:', cleanupErr);
}
});
});
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server running on port ${PORT}`);
});
```
These changes:
1. Added Axios for backend API calls
2. Created main.js with complete application logic
3. Added server.js with backend API implementation
4. Updated index.html to include the new JavaScript files
The backend uses Express.js with endpoints for:
- Uploading invitation designs
- Uploading guest lists
- Generating invitations
- Downloading the final ZIP file
Note: You'll need to install the required Node.js packages:
```
npm install express multer cors adm-zip uuid
```