diff --git a/app.js b/app.js index 568ffe3..bbe926c 100644 --- a/app.js +++ b/app.js @@ -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}`); -}); \ No newline at end of file + console.log(`Server running on port ${port}`); +}); + +module.exports = app; \ No newline at end of file diff --git a/package.json b/package.json index 17d7865..f57245c 100644 --- a/package.json +++ b/package.json @@ -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" } -} \ No newline at end of file +} \ No newline at end of file diff --git a/src/controllers/ethPriceController.js b/src/controllers/ethPriceController.js new file mode 100644 index 0000000..a17cb9a --- /dev/null +++ b/src/controllers/ethPriceController.js @@ -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; \ No newline at end of file diff --git a/src/routes/ethPriceRoutes.js b/src/routes/ethPriceRoutes.js new file mode 100644 index 0000000..3733e07 --- /dev/null +++ b/src/routes/ethPriceRoutes.js @@ -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; \ No newline at end of file diff --git a/src/services/ethPriceService.js b/src/services/ethPriceService.js new file mode 100644 index 0000000..ca2e0af --- /dev/null +++ b/src/services/ethPriceService.js @@ -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; \ No newline at end of file diff --git a/tests/ethPriceEndpoint.test.js b/tests/ethPriceEndpoint.test.js new file mode 100644 index 0000000..0a76f45 --- /dev/null +++ b/tests/ethPriceEndpoint.test.js @@ -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); + }); +}); \ No newline at end of file