初始化專案:租屋契約 PDF 產生器

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-15 12:04:54 +08:00
commit e1fcf3eb77
13 changed files with 1768 additions and 0 deletions

60
server.js Normal file
View File

@@ -0,0 +1,60 @@
const express = require('express');
const path = require('node:path');
const {
createContractPdf,
listTemplates,
} = require('./src/contractService');
const app = express();
const port = Number(process.env.PORT || 3005);
app.use(express.json({ limit: '1mb' }));
app.use(express.static(path.join(__dirname, 'public')));
app.get('/api/health', (_req, res) => {
res.json({ ok: true });
});
app.get('/api/templates', async (_req, res, next) => {
try {
res.json({ templates: await listTemplates() });
} catch (error) {
next(error);
}
});
app.post('/api/contracts/pdf', async (req, res) => {
try {
const result = await createContractPdf({
template: req.body.template,
monthlyRent: req.body.monthlyRent,
paymentDay: req.body.paymentDay,
deposit: req.body.deposit,
});
res.setHeader('Content-Type', 'application/pdf');
res.setHeader(
'Content-Disposition',
`attachment; filename="rental-contract.pdf"; filename*=UTF-8''${encodeURIComponent(result.fileName)}`,
);
res.setHeader('Cache-Control', 'no-store');
res.send(result.buffer);
} catch (error) {
console.error(error);
const status = error.exposeToClient ? 400 : 500;
res.status(status).json({
error: error.exposeToClient
? error.message
: 'PDF 產生失敗,請確認 LibreOffice 已安裝且範本格式正確。',
});
}
});
app.use((error, _req, res, _next) => {
console.error(error);
res.status(500).json({ error: '伺服器發生錯誤。' });
});
app.listen(port, () => {
console.log(`Rental contract PDF app is running at http://localhost:${port}`);
});