-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
47 lines (40 loc) · 1.11 KB
/
index.js
File metadata and controls
47 lines (40 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const express = require('express')
const axios = require('axios')
const cors = require('cors')
const app = express()
// Get the quotes api from the environment(refer docker-compose.yml)
const QUOTES_API_GATEWAY = process.env.QUOTES_API
// Use CORS to prevent Cross-Origin Requets issue
app.use(cors())
// Get the status of the API
app.get('/api/status', (req, res) => {
return res.json({status: 'ok'})
})
// Returns a random quote from the quote api
app.get('/api/randomquote',async (req, res) => {
try {
const url = QUOTES_API_GATEWAY + '/api/quote'
const quote = await axios.get(url)
return res.json({
time: Date.now(),
quote: quote.data
})
} catch (error) {
console.log(error)
res.status(500)
return res.json({
message: "Internal server error",
})
}
})
// Handle any unknown route
app.get('*', (req, res) => {
res.status(404)
return res.json({
message: 'Resource not found'
})
});
// starts the app
app.listen(3000, () => {
console.log('API Gateway is listening on port 3000!')
})