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
13 changes: 7 additions & 6 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
const express = require('express');
const app = express();
const port = 3000;
const ethPriceRoutes = require('./src/routes/ethPriceRoutes');

app.get('/', (req, res) => {
res.send('Hello World!');
});
app.use('/api', ethPriceRoutes);

const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`Listening on http://localhost:${port}`);
});
console.log(`Server running on port ${port}`);
});

module.exports = app;
14 changes: 10 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
{
"name": "express-hello-world",
"version": "1.0.0",
"description": "一个简单的 Express Hello World 应用",
"description": "Express application with ETH price endpoint",
"main": "app.js",
"scripts": {
"start": "node app.js"
"start": "node app.js",
"test": "jest"
},
"dependencies": {
"express": "^4.18.2"
"express": "^4.18.2",
"node-fetch": "^2.6.1"
},
"devDependencies": {
"jest": "^27.0.0",
"supertest": "^6.1.6"
}
}
}
14 changes: 14 additions & 0 deletions src/controllers/ethPriceController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const EthPriceService = require('../services/ethPriceService');

class EthPriceController {
static async getEthPrice(req, res) {
try {
const ethPrice = await EthPriceService.getEthPrice();
res.json({ price: ethPrice });
} catch (error) {
res.status(500).json({ error: error.message });
}
}
}

module.exports = EthPriceController;
8 changes: 8 additions & 0 deletions src/routes/ethPriceRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require('express');
const EthPriceController = require('../controllers/ethPriceController');

const router = express.Router();

router.get('/ethprice', EthPriceController.getEthPrice);

module.exports = router;
23 changes: 23 additions & 0 deletions src/services/ethPriceService.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const fetch = require('node-fetch');

class EthPriceService {
static async getEthPrice() {
const COINGECKO_API_URL = 'https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd';

try {
const response = await fetch(COINGECKO_API_URL);

if (!response.ok) {
throw new Error('Unable to fetch Ethereum price');
}

const data = await response.json();
return data.ethereum.usd;
} catch (error) {
console.error('Error fetching ETH price:', error);
throw new Error('Failed to retrieve Ethereum price');
}
}
}

module.exports = EthPriceService;
13 changes: 13 additions & 0 deletions tests/ethPriceEndpoint.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const request = require('supertest');
const app = require('../app');

describe('ETH Price Endpoint', () => {
it('should return a valid ETH price', async () => {
const response = await request(app).get('/api/ethprice');

expect(response.statusCode).toBe(200);
expect(response.body).toHaveProperty('price');
expect(typeof response.body.price).toBe('number');
expect(response.body.price).toBeGreaterThan(0);
});
});