-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
70 lines (62 loc) · 2.05 KB
/
Copy pathserver.js
File metadata and controls
70 lines (62 loc) · 2.05 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
require('dotenv').config();
const express = require('express');
const fs = require('fs');
const path = require('path');
const port = process.env.PORT || 3000;
const app = express();
const invoiceData = {
companyDetails: {
address: "PO Box XXXX, Dubai, United Arab Emirates",
phone: "+971 xx xxxxxx",
email: "email@email.com",
trn: "XXXXXXXXXXXXX"
},
invoice: {
clientId: "CIN 10009",
invoiceNo: "INV-10000027",
invoiceDate: "-",
sailingDate: "May 31, 2024",
vessel: "Mv Ghazi",
voyage: "GH2001",
portOfLoading: "PKKHI",
portOfDischarge: "OMSOH"
},
billing: {
billedTo: "Nagasaki",
trn: "-",
address: "-",
bookingParty: "Nagasaki",
quotedParty: "Nagasaki",
polAgent: "Modern Shipping Agencies",
podAgent: "Federal Shipping Agents Private Limited"
}
};
// Serve static files (e.g., logo images)
app.use(express.static('public'));
// Endpoint to serve the HTML
app.get('/invoice', (req, res) => {
const templatePath = path.join(__dirname, 'invoice.html');
// Read the HTML template
fs.readFile(templatePath, 'utf8', (err, htmlContent) => {
if (err) {
return res.status(500).send('Error loading the template');
}
// Replace placeholders with actual data
Object.keys(invoiceData).forEach(key => {
if (typeof invoiceData[key] === 'object') {
Object.keys(invoiceData[key]).forEach(subKey => {
const placeholder = `{{${key}.${subKey}}}`;
htmlContent = htmlContent.replace(new RegExp(placeholder, 'g'), invoiceData[key][subKey]);
});
} else {
const placeholder = `{{${key}}}`;
htmlContent = htmlContent.replace(new RegExp(placeholder, 'g'), invoiceData[key]);
}
});
res.send(htmlContent);
});
});
// Start the server
app.listen(port, () => {
console.log(`Server is running on http://localhost:${port}`);
});