Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PORT=4000
4 changes: 0 additions & 4 deletions backend/.gitignore

This file was deleted.

10 changes: 10 additions & 0 deletions backend/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM node:alpine
WORKDIR /app/backend
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 4000
CMD ["node", "index.js"]



5 changes: 5 additions & 0 deletions backend/config/allowedOrigins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const allowedOrigins = [
'http://localhost:3000',
]

module.exports = allowedOrigins;
15 changes: 15 additions & 0 deletions backend/config/corsOptions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const allowedOrigins = require('./allowedOrigins')

const corsOptions = {
origin: (origin, callback) => {
if (allowedOrigins.indexOf(origin) !== -1) {
callback(null, true)
} else {
callback(new Error('Not allowed by CORS'))
}
},
credentials: true,
optionsSuccessStatus: 200
}

module.exports = corsOptions
52 changes: 52 additions & 0 deletions backend/controllers/exchangeControllers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const axios = require("axios");

const exchange = async (req, res) => {
const { coin, currency } = req.query;

try {
// Fetch the markets for the specified coin
const response = await axios.get(
`https://api.coinstats.app/public/v1/markets?coinId=${coin}&currency=${currency}`
);
const data = response.data;

const markets = data;

if (!markets || markets.length === 0) {
res
.status(404)
.json({ error: "No markets found for the specified coin" });
return;
}

if (markets.length === 0) {
res
.status(404)
.json({ error: "No markets found for the selected currency" });
} else if (markets.length === 1) {
// Only one market supports the selected currency
const exchange = markets[0].exchange;
res.json({ exchange });
} else {
// Multiple markets support the selected currency, find the one with the lowest price
let minPrice = Number.MAX_VALUE;
let minPriceExchange = "";

markets.forEach((market) => {
if (market.price < minPrice) {
minPrice = market.price;
minPriceExchange = market.exchange;
}
});

res.json({ exchange: minPriceExchange });
}
} catch (error) {
console.error("Error fetching markets:", error);
res.status(500).json({ error: "Internal server error" });
}
};

module.exports = {
exchange,
};
16 changes: 16 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require("dotenv").config();
const express = require("express");
const cors = require('cors')
const corsOptions = require('./config/corsOptions')
const app = express();
const PORT = process.env.PORT || 4000;


app.use(cors(corsOptions))

app.use(require("./routes/exchangeRoutes"));


app.listen(PORT, () => {
console.log(`Backend server is running on port ${PORT}`);
});
Loading