..
Viewing
server.ts
108 lines (95 loc) • 3.1 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 | import express from 'express';
import axios from 'axios';
import path from 'path';
import os from 'os';
import bodyParser from 'body-parser';
const app = express();
// Environment Configuration
const PORT = process.env.VITE_PORT || 5173;
const {
VITE_CLIENT_ID: CLIENT_ID,
VITE_CLIENT_SECRET: CLIENT_SECRET,
VITE_REDIRECT_URI: REDIRECT_URI,
} = process.env;
// Directory paths for static assets
const DIST_DIR = path.join(__dirname, '../dist');
const INDEX_FILE = path.join(DIST_DIR, 'index.html');
// Middleware for static assets and JSON parsing
app.use(express.static(DIST_DIR));
app.use(express.json());
app.use(bodyParser.json());
// API Endpoint for exchanging authorization token
const apiEndpoint = '/api/exchange-token';
app.post(apiEndpoint, async (req, res) => {
const { code } = req.body;
if (!code) {
console.error('Authorization code is missing');
return res.status(400).send('Authorization code is required');
}
const payload = {
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
code,
grant_type: 'authorization_code',
redirect_uri: REDIRECT_URI,
};
const url = 'https://anilist.co/api/v2/oauth/token';
// Logging the request details
console.log('Sending request to AniList API');
console.log('URL:', url);
console.log('Payload:', payload);
try {
const response = await axios.post(url, payload, {
headers: {
'Content-Type': 'application/json',
'Accept-Encoding': 'identity',
},
});
// Logging the response details
console.log('Received response from AniList API');
console.log('Response Status:', response.status);
console.log('Response Data:', response.data);
if (response.data.access_token) {
res.json({ accessToken: response.data.access_token });
} else {
throw new Error('Access token not found in the response');
}
} catch (error) {
console.error('Error during token exchange:', error.message);
if (error.response) {
console.error('Error Status:', error.response.status);
console.error('Error Details:', error.response.data);
}
res.status(500).json({
error: 'Failed to exchange token',
details: error.response?.data || error.message,
});
}
});
// Serve the main index.html for any non-API requests
app.get('*', (req, res) => {
res.sendFile(INDEX_FILE, (err) => {
if (err) {
console.error('Error serving index.html:', err);
res.status(500).send('An error occurred while serving the application');
}
});
});
// Utility to get the first non-internal IPv4 address
function getLocalIpAddress() {
const networkInterfaces = os.networkInterfaces();
for (const networkInterface of Object.values(networkInterfaces)) {
const found = networkInterface?.find(
(net) => net.family === 'IPv4' && !net.internal,
);
if (found) return found.address;
}
return 'localhost';
}
// Starting the server
app.listen(PORT, () => {
const ipAddress = getLocalIpAddress();
console.log(
`Server is running at:\n- Localhost: http://localhost:${PORT}\n- Local IP: http://${ipAddress}:${PORT}`,
);
});
|
|