-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
109 lines (90 loc) · 2.97 KB
/
server.js
File metadata and controls
109 lines (90 loc) · 2.97 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
const express = require('express');
const path = require('path');
const fetch = require('node-fetch');
const app = express();
const PORT = process.env.PORT || 3000;
// Add some basic middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
// Log all requests
app.use((req, res, next) => {
console.log(`${new Date().toISOString()} - ${req.method} ${req.path}`);
next();
});
// Serve static files from the dist directory
app.use(express.static(path.join(__dirname, 'dist')));
// Health check endpoint
app.get('/health', (req, res) => {
res.status(200).json({
status: 'OK',
timestamp: new Date().toISOString(),
port: PORT,
environment: process.env.NODE_ENV || 'development'
});
});
// Test endpoint
app.get('/test', (req, res) => {
res.status(200).json({
message: 'PDF Flipbook server is running!',
timestamp: new Date().toISOString(),
port: PORT
});
});
// PDF proxy endpoint to bypass CORS
app.get('/proxy/pdf', async (req, res) => {
try {
const pdfUrl = req.query.url;
if (!pdfUrl) {
return res.status(400).json({ error: 'PDF URL is required' });
}
console.log('Proxying PDF request to:', pdfUrl);
const response = await fetch(pdfUrl, {
headers: {
'User-Agent': 'Mozilla/5.0 (compatible; PDF-Flipbook/1.0)'
}
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
const buffer = await response.buffer();
res.setHeader('Content-Type', 'application/pdf');
res.setHeader('Content-Length', buffer.length);
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
res.send(buffer);
} catch (error) {
console.error('PDF proxy error:', error);
res.status(500).json({
error: 'Failed to proxy PDF',
details: error.message
});
}
});
// Handle all routes by serving index.html (for SPA routing)
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});
// Error handling middleware
app.use((err, req, res, next) => {
console.error('Error:', err);
res.status(500).json({ error: 'Internal Server Error' });
});
const server = app.listen(PORT, '0.0.0.0', () => {
console.log(`🚀 PDF Flipbook server running on port ${PORT}`);
console.log(`📖 Visit: http://localhost:${PORT}`);
console.log(`🌍 Environment: ${process.env.NODE_ENV || 'development'}`);
console.log(`🔗 Railway URL: ${process.env.RAILWAY_STATIC_URL || 'Not set'}`);
console.log(`🔧 Environment variables:`, {
PORT: process.env.PORT,
NODE_ENV: process.env.NODE_ENV,
RAILWAY_STATIC_URL: process.env.RAILWAY_STATIC_URL
});
});
// Handle graceful shutdown
process.on('SIGTERM', () => {
console.log('SIGTERM received, shutting down gracefully');
server.close(() => {
console.log('Process terminated');
});
});