OwlCyberSecurity - MANAGER
Edit File: app.js
import express from 'express'; import cors from 'cors'; import dotenv from 'dotenv'; import { fileURLToPath } from 'url'; import { dirname, join } from 'path'; import pool from './db.js'; import router from './routes/asset.routes.js'; import loginrouter from './routes/auth.routes.js'; import dashboardrouter from './routes/dashboard.routes.js'; import tableRoutes from './routes/table.routes.js'; import searchroutes from './routes/search.routes.js'; import createtableroutes from './routes/createtable.routes.js'; // Load environment variables dotenv.config(); // Get current directory const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); // Initialize Express App const app = express(); app.use((req, res, next) => { res.set('Cache-Control', 'no-store'); next(); }); // Middleware app.use(express.static(join(__dirname, 'Uploads'))); app.use(express.json()); app.use(cors()); // Routes app.use('/api/assets', router); app.use('/api', loginrouter); app.use('/api', dashboardrouter); app.use('/api', tableRoutes); app.use('/api', searchroutes); app.use('/api', createtableroutes); // PUT app.put('/api/table/:table/:id', async (req, res) => { const { table, id } = req.params; const updatedData = req.body; await pool.query(`UPDATE ?? SET ? WHERE id = ?`, [table, updatedData, id]); const [rows] = await pool.query(`SELECT * FROM ?? WHERE id = ?`, [table, id]); res.json(rows[0]); }); // DELETE app.delete('/api/table/:table/:id', async (req, res) => { const { table, id } = req.params; await pool.query(`DELETE FROM ?? WHERE id = ?`, [table, id]); res.json({ message: 'Deleted' }); }); // Error Handling app.use((err, req, res, next) => { console.error('🔥 Unhandled error:', err); res.status(err.status || 500).json({ message: err.message || 'Server Error', stack: process.env.NODE_ENV === 'production' ? undefined : err.stack, }); }); // Start Server const PORT = process.env.PORT || 2005; const HOST = 'testone.onebox.pk'; pool .getConnection() .then((connection) => { console.log('✅ Database connected...'); connection.release(); app.listen(PORT, '0.0.0.0', () => { console.log(`🚀 Server running at http://${HOST}:${PORT}`); }); }) .catch((err) => { console.error('❌ Database connection error:', err); process.exit(1); }); export default app;