Skip to content

Commit

Permalink
Create endpoints.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Dec 3, 2024
1 parent 1310fc0 commit f99fcb8
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/api/endpoints.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// api/endpoints.js
const express = require('express');
const APIIntegration = require('./integration');

const router = express.Router();
const apiClient = new APIIntegration('https://api.example.com'); // Replace with actual API base URL

// Middleware for logging requests
router.use((req, res, next) => {
console.log(`${req.method} request for '${req.url}'`);
next();
});

// Example endpoint to get user data
router.get('/users/:id', async (req, res) => {
try {
const userId = req.params.id;
const userData = await apiClient.get(`/users/${userId}`);
res.status(200).json(userData);
} catch (error) {
res.status(500).json({ error: error.message });
}
});

// Example endpoint to create a new user
router.post('/users', async (req, res) => {
try {
const newUser = req.body;
const createdUser = await apiClient.post('/users', newUser);
res.status(201).json(createdUser);
} catch (error) {
res.status(500).json({ error: error.message });
}
});

// Example endpoint to update user data
router.put('/users/:id', async (req, res) => {
try {
const userId = req.params.id;
const updatedUser = req.body;
const response = await apiClient.post(`/users/${userId}`, updatedUser);
res.status(200).json(response);
} catch (error) {
res.status(500).json({ error: error.message });
}
});

// Example endpoint to delete a user
router.delete('/users/:id', async (req, res) => {
try {
const userId = req.params.id;
await apiClient.delete(`/users/${userId}`);
res.status(204).send();
} catch (error) {
res.status(500).json({ error: error.message });
}
});

module.exports = router;

0 comments on commit f99fcb8

Please sign in to comment.