key
This commit is contained in:
parent
8f190212ca
commit
caff5271b8
File diff suppressed because one or more lines are too long
@ -104,7 +104,7 @@ app.use('/api/file', fileRoutes);
|
|||||||
app.use('/api/pexels', pexelsRoutes);
|
app.use('/api/pexels', pexelsRoutes);
|
||||||
app.enable('trust proxy');
|
app.enable('trust proxy');
|
||||||
app.use('/api/exchange', exchangeRoutes);
|
app.use('/api/exchange', exchangeRoutes);
|
||||||
app.use('/api/weather', weatherRoutes);
|
app.use('/exchange', exchangeRoutes);
|
||||||
|
|
||||||
|
|
||||||
app.use(
|
app.use(
|
||||||
|
|||||||
@ -5,13 +5,18 @@ const router = express.Router();
|
|||||||
// GET /api/exchange?base=USD
|
// GET /api/exchange?base=USD
|
||||||
router.get('/', async (req, res) => {
|
router.get('/', async (req, res) => {
|
||||||
try {
|
try {
|
||||||
const base = req.query.base || 'USD';
|
const apiKey = process.env.EXCHANGE_API_KEY;
|
||||||
const response = await axios.get(
|
const baseCurrency = req.query.base || 'USD';
|
||||||
`https://api.exchangerate.host/latest?base=${encodeURIComponent(base)}&symbols=USD,EUR`
|
if (apiKey) {
|
||||||
);
|
const url = `https://api.apilayer.com/exchangerates_data/latest?base=${encodeURIComponent(baseCurrency)}&symbols=USD,EUR`;
|
||||||
// Return only necessary data
|
const response = await axios.get(url, { headers: { apikey: apiKey } });
|
||||||
const { base: responseBase, rates, date } = response.data;
|
return res.json(response.data);
|
||||||
res.json({ base: responseBase, rates, date });
|
} else {
|
||||||
|
const url = `https://api.exchangerate.host/latest?base=${encodeURIComponent(baseCurrency)}&symbols=USD,EUR`;
|
||||||
|
const response = await axios.get(url);
|
||||||
|
return res.json(response.data);
|
||||||
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Exchange route error:', error.message);
|
console.error('Exchange route error:', error.message);
|
||||||
res.status(500).json({ error: 'Failed to fetch exchange rates' });
|
res.status(500).json({ error: 'Failed to fetch exchange rates' });
|
||||||
|
|||||||
28
backend/src/routes/exchange.js.temp
Normal file
28
backend/src/routes/exchange.js.temp
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
const express = require('express');
|
||||||
|
const axios = require('axios');
|
||||||
|
const router = express.Router();
|
||||||
|
|
||||||
|
// GET /api/exchange?base=USD
|
||||||
|
router.get('/', async (req, res) => {
|
||||||
|
try {
|
||||||
|
const apiKey = process.env.EXCHANGE_API_KEY;
|
||||||
|
const baseCurrency = req.query.base || 'USD';
|
||||||
|
if (apiKey) {
|
||||||
|
const url = `https://api.apilayer.com/exchangerates_data/latest?base=${encodeURIComponent(baseCurrency)}&symbols=USD,EUR`;
|
||||||
|
const response = await axios.get(url, { headers: { apikey: apiKey } });
|
||||||
|
return res.json(response.data);
|
||||||
|
}
|
||||||
|
const url = `https://api.exchangerate.host/latest?base=${encodeURIComponent(baseCurrency)}&symbols=USD,EUR`;
|
||||||
|
const response = await axios.get(url);
|
||||||
|
return res.json(response.data);
|
||||||
|
const response = await axios.get(url);
|
||||||
|
return res.json(response.data);
|
||||||
|
}
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error('Exchange route error:', error.message);
|
||||||
|
res.status(500).json({ error: 'Failed to fetch exchange rates' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
module.exports = router;
|
||||||
@ -15,8 +15,7 @@ export interface ExchangeData {
|
|||||||
export const getExchangeRates = async (
|
export const getExchangeRates = async (
|
||||||
base: string
|
base: string
|
||||||
): Promise<ExchangeData> => {
|
): Promise<ExchangeData> => {
|
||||||
const response: AxiosResponse<ExchangeData> = await axios.get(
|
const response: AxiosResponse<ExchangeData> = await axios.get(`/exchange?base=${encodeURIComponent(base)}`);
|
||||||
`/exchange?base=${encodeURIComponent(base)}`
|
|
||||||
);
|
|
||||||
return response.data;
|
return response.data;
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user