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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| const express = require('express'); const oracledb = require('oracledb'); const app = express(); const PORT = process.env.PORT || 3000;
const dbConfig = { user: 'apps', password: 'apps', connectString: 'erpdev.sercomm.com:1523/DEV', };
if (process.platform === 'win32' || (process.platform === 'darwin' && process.arch === 'x64')) { oracledb.initOracleClient({ libDir: 'C:/Program Files/instantclient_11_2' }); }
app.use(express.json());
app.use(async (req, res, next) => { req.db = await oracledb.getConnection(dbConfig); next(); });
app.get('/api/posts', async (req, res) => { try { const result = await req.db.execute( `SELECT * FROM posts`, [], { outFormat: oracledb.OUT_FORMAT_OBJECT } ); res.json(result.rows); } catch (err) { console.error(err.message); res.status(500).send('Server Error'); } });
app.get('/api/posts/:id', async (req, res) => { const { id } = req.params; try { const result = await req.db.execute( `SELECT * FROM posts WHERE id = :id`, [id], { outFormat: oracledb.OUT_FORMAT_OBJECT } );
if (result.rows.length === 0) { return res.status(404).json({ message: 'Post not found' }); }
res.json(result.rows[0]); } catch (err) { console.error(err.message); res.status(500).send('Server Error'); } });
app.post('/api/posts', async (req, res) => { const { title, content } = req.body; try { await req.db.execute( `INSERT INTO posts (title, content) VALUES (:title, :content)`, [title, content], { autoCommit: true } ); res.send('Post created successfully'); } catch (err) { console.error(err.message); res.status(500).send('Server Error'); } });
app.put('/api/posts/:id', async (req, res) => { const { title, content } = req.body; const { id } = req.params; try { await req.db.execute( `UPDATE posts SET title = :title, content = :content WHERE id = :id`, [title, content, id], { autoCommit: true } ); res.send('Post updated successfully'); } catch (err) { console.error(err.message); res.status(500).send('Server Error'); } });
app.delete('/api/posts/:id', async (req, res) => { const { id } = req.params; try { await req.db.execute( `DELETE FROM posts WHERE id = :id`, [id], { autoCommit: true } ); res.send('Post deleted successfully'); } catch (err) { console.error(err.message); res.status(500).send('Server Error'); } });
app.listen(PORT, () => { console.log(`Server running on port ${PORT}`); });
|