forked from Shineii86/AniNewsAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
117 lines (104 loc) · 3.42 KB
/
Copy pathserver.js
File metadata and controls
117 lines (104 loc) · 3.42 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
110
111
112
113
114
115
116
117
const express = require('express');
const path = require('path');
const cacheHandler = require('./utils/cacheHandler');
const app = express();
const PORT = process.env.PORT || 3000;
// Middleware
app.use(express.json());
app.use(express.static('public'));
// Request logging middleware
app.use((req, res, next) => {
console.log(`[${new Date().toISOString()}] ${req.method} ${req.path}`);
next();
});
// API routes
app.get('/api/news', require('./api/news.js'));
app.get('/api/news/tags', require('./api/news/tags.js'));
app.get('/api/news/:slug', require('./api/news/[slug].js'));
// Health check endpoint
app.get('/api/health', (req, res) => {
const stats = cacheHandler.getStats();
res.json({
success: true,
status: 'healthy',
uptime: process.uptime(),
timestamp: new Date().toISOString(),
cache: stats,
version: '2.0.0'
});
});
// Cache stats endpoint
app.get('/api/stats', (req, res) => {
const stats = cacheHandler.getStats();
res.json({
success: true,
data: stats
});
});
// Clear cache endpoint (for admin use)
app.post('/api/cache/clear', (req, res) => {
const { key } = req.body;
if (key) {
cacheHandler.del(key);
res.json({
success: true,
message: `Cache key '${key}' cleared`
});
} else {
cacheHandler.flush();
res.json({
success: true,
message: 'All caches cleared'
});
}
});
// Root route
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, 'public', 'index.html'));
});
// 404 handler
app.use((req, res) => {
res.status(404).json({
success: false,
error: 'Not found',
message: 'The requested endpoint does not exist',
availableEndpoints: [
'/api/news',
'/api/news/tags',
'/api/news/:slug',
'/api/health',
'/api/stats'
]
});
});
// Error handler
app.use((err, req, res, next) => {
console.error('[Server Error]:', err);
res.status(500).json({
success: false,
error: 'Internal server error',
message: err.message
});
});
app.listen(PORT, () => {
console.log(`
╔════════════════════════════════════════════════════════════╗
║ ║
║ 🎌 Anime News API v2.0.0 🎌 ║
║ ║
║ Server running on http://localhost:${PORT} ║
║ ║
║ Endpoints: ║
║ • GET /api/news - Get all news ║
║ • GET /api/news/tags - Filter by tags ║
║ • GET /api/news/:slug - Get article by slug ║
║ • GET /api/health - Health check ║
║ • GET /api/stats - Cache statistics ║
║ ║
║ Sources: ANN, AnimeCorner, MAL, OtakuUSA, ║
║ Crunchyroll, AnimeHerald, ComicBook ║
║ ║
╚════════════════════════════════════════════════════════════╝
`);
});
module.exports = app;