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.enable('trust proxy');
|
||||
app.use('/api/exchange', exchangeRoutes);
|
||||
app.use('/api/weather', weatherRoutes);
|
||||
app.use('/exchange', exchangeRoutes);
|
||||
|
||||
|
||||
app.use(
|
||||
|
||||
@ -5,13 +5,18 @@ const router = express.Router();
|
||||
// GET /api/exchange?base=USD
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const base = req.query.base || 'USD';
|
||||
const response = await axios.get(
|
||||
`https://api.exchangerate.host/latest?base=${encodeURIComponent(base)}&symbols=USD,EUR`
|
||||
);
|
||||
// Return only necessary data
|
||||
const { base: responseBase, rates, date } = response.data;
|
||||
res.json({ base: responseBase, rates, date });
|
||||
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);
|
||||
} 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) {
|
||||
console.error('Exchange route error:', error.message);
|
||||
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 (
|
||||
base: string
|
||||
): Promise<ExchangeData> => {
|
||||
const response: AxiosResponse<ExchangeData> = await axios.get(
|
||||
`/exchange?base=${encodeURIComponent(base)}`
|
||||
);
|
||||
const response: AxiosResponse<ExchangeData> = await axios.get(`/exchange?base=${encodeURIComponent(base)}`);
|
||||
|
||||
return response.data;
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user