-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiGateway.js
34 lines (29 loc) · 986 Bytes
/
apiGateway.js
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
const express = require('express');
const axios = require('axios');
const app = express();
app.use(express.json());
const ATTENDANCE_SERVICE_URL = 'http://localhost:3001/attendance';
const USER_SERVICE_URL = 'http://localhost:3002/users';
app.post('/attendance', async (req, res) => {
try {
const { userId, timestamp } = req.body;
await axios.post(ATTENDANCE_SERVICE_URL, { userId, timestamp });
res.status(200).send('Attendance recorded successfully');
} catch (error) {
console.error('Error recording attendance:', error);
res.status(500).send('Error recording attendance');
}
});
app.get('/users', async (req, res) => {
try {
const users = await axios.get(USER_SERVICE_URL);
res.status(200).json(users.data);
} catch (error) {
console.error('Error fetching users:', error);
res.status(500).send('Error fetching users');
}
});
const PORT = 4000;
app.listen(PORT, () => {
console.log(`API Gateway listening on port ${PORT}`);
});