endpoint.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const express = require('express');
  2. const { Client } = require('pg');
  3. const bodyParser = require('body-parser');
  4. const app = express();
  5. // Middleware to parse JSON bodies
  6. app.use(bodyParser.json());
  7. // PostgreSQL client setup
  8. const client = new Client({
  9. user: 'app',
  10. host: 'localhost',
  11. database: 'app',
  12. password: 'app',
  13. port: 5432,
  14. });
  15. // Connect to PostgreSQL
  16. client.connect()
  17. .then(() => console.log('Connected to PostgreSQL'))
  18. .catch(err => console.error('Connection error', err.stack));
  19. // Test connection route
  20. app.get('/apps/mysql/test', async (req, res) => {
  21. try {
  22. await client.query('SELECT NOW()');
  23. res.json({ message: 'Connection successful' });
  24. } catch (err) {
  25. res.status(500).json({ error: 'Failed to connect' });
  26. }
  27. });
  28. // Create endpoint - insert a name-value pair
  29. app.post('/apps/mysql/create', async (req, res) => {
  30. const { name, value } = req.body; // Expecting {"name": "value"}
  31. if (!name || !value) {
  32. return res.status(400).json({ error: 'Name and value required' });
  33. }
  34. try {
  35. await client.query('INSERT INTO your_table_name (name, value) VALUES ($1, $2)', [name, value]);
  36. res.json({ message: 'Record created successfully' });
  37. } catch (err) {
  38. res.status(500).json({ error: 'Error inserting record' });
  39. }
  40. });
  41. // Read endpoint - fetch fields based on id and specified fields
  42. app.get('/apps/mysql/read/:id/:fields*', async (req, res) => {
  43. const { id } = req.params;
  44. const fields = req.params.fields.split('/'); // Get all fields after /id
  45. if (!id || fields.length === 0) {
  46. return res.status(400).json({ error: 'ID and fields required' });
  47. }
  48. try {
  49. const fieldNames = fields.join(', ');
  50. const result = await client.query(`SELECT ${fieldNames} FROM your_table_name WHERE id = $1`, [id]);
  51. if (result.rows.length === 0) {
  52. return res.status(404).json({ error: 'Record not found' });
  53. }
  54. res.json(result.rows[0]);
  55. } catch (err) {
  56. res.status(500).json({ error: 'Error fetching record' });
  57. }
  58. });
  59. // Update endpoint - update record by id with payload
  60. app.put('/apps/mysql/update/:id', async (req, res) => {
  61. const { id } = req.params;
  62. const updates = req.body; // Expecting a JSON payload like {"name": "newValue"}
  63. if (!id || Object.keys(updates).length === 0) {
  64. return res.status(400).json({ error: 'ID and updates required' });
  65. }
  66. const updateFields = Object.keys(updates)
  67. .map((key, index) => `${key} = $${index + 2}`)
  68. .join(', ');
  69. const values = [id, ...Object.values(updates)];
  70. try {
  71. await client.query(`UPDATE your_table_name SET ${updateFields} WHERE id = $1`, values);
  72. res.json({ message: 'Record updated successfully' });
  73. } catch (err) {
  74. res.status(500).json({ error: 'Error updating record' });
  75. }
  76. });
  77. // Delete endpoint - delete record by id
  78. app.delete('/apps/mysql/delete/:id', async (req, res) => {
  79. const { id } = req.params;
  80. if (!id) {
  81. return res.status(400).json({ error: 'ID required' });
  82. }
  83. try {
  84. await client.query('DELETE FROM your_table_name WHERE id = $1', [id]);
  85. res.json({ message: 'Record deleted successfully' });
  86. } catch (err) {
  87. res.status(500).json({ error: 'Error deleting record' });
  88. }
  89. });
  90. // Start the server and listen on port 3000
  91. const port = 15012;
  92. app.listen(port, '0.0.0.0', () => {
  93. console.log(`Server running at http://localhost:${port}/apps/mysql/test`);
  94. });