-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
114 lines (92 loc) · 3.88 KB
/
app.js
File metadata and controls
114 lines (92 loc) · 3.88 KB
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
/*import express from 'express';
import cors from 'cors';
import 'dotenv/config';
// FIX 1: Paths corrected to look for modules relative to the root/app.js location.
import authRoutes from './routes/auth.js';
import fileRoutes from './routes/file.js';
// FIX 2: Corrected path for db.js which is now a sibling file to app.js
import pool from './db.js';
const app = express();
// Load port from .env or default to 3000
const PORT = process.env.PORT || 3000;
// Middleware
app.use(cors());
app.use(express.json()); // Body parser for JSON requests
// Basic server test route
app.get('/', (req, res) => {
res.status(200).json({ message: 'Secure File Drive API is running successfully!' });
});
// Route Handlers
app.use('/api/auth', authRoutes);
app.use('/api/files', fileRoutes);
// --- Database Check ---
// Function to verify the database connection on server startup
async function checkDatabaseConnection() {
try {
// Run a simple query to ensure the pool is working
await pool.query('SELECT 1');
console.log('✅ Database connected successfully!');
} catch (err) {
console.error('❌ Database connection failed:', err.message);
console.error('Ensure PostgreSQL is running and your .env file is configured correctly.');
}
}
// Start Server
app.listen(PORT, async () => {
console.log(`🚀 Server running on port ${PORT}`);
// Check database connection immediately after the server starts listening
await checkDatabaseConnection();
});
*/
import express from 'express';
import cors from 'cors';
import 'dotenv/config';
import path from 'path'; // Needed to join paths reliably
import { fileURLToPath } from 'url'; // Needed to get the current directory in ES Modules
// Helper to define __dirname equivalent in ES Modules
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// --- 1. FIX: Corrected file names and paths for Route Handlers ---
// Routes files are named 'authRoute.js' and 'files.js'
import authRoutes from './routes/auth.js';
import fileRoutes from './routes/file.js';
// --- 2. FIX: Corrected path for db.js ---
// Assuming db.js is now inside the 'database' folder at the root.
import pool from './db.js';
const app = express();
// Load port from .env or default to 3000
const PORT = process.env.PORT || 3000;
// Middleware
app.use(cors());
app.use(express.json()); // Body parser for JSON requests
// --- 3. FIX: Add Static File Middleware to serve pages and client assets ---
// This middleware allows Express to serve HTML, CSS, JS, etc., from the project root.
// This resolves the "Cannot GET /" and "Cannot GET /index.html" errors.
// If an index.html file exists in the root, it will be served automatically for the '/' route.
app.use(express.static(__dirname)); // Serves static files from the root directory
// Basic server test route: Use /api or /status to check server health,
// leaving '/' free for the static index.html file.
app.get('/status', (req, res) => {
res.status(200).json({ message: 'Secure File Drive API is running successfully!' });
});
// Route Handlers
app.use('/api/auth', authRoutes);
app.use('/api/files', fileRoutes);
// --- Database Check ---
// Function to verify the database connection on server startup
async function checkDatabaseConnection() {
try {
// Run a simple query to ensure the pool is working
await pool.query('SELECT 1');
console.log('✅ Database connected successfully!');
} catch (err) {
console.error('❌ Database connection failed:', err.message);
console.error('Ensure PostgreSQL is running and your .env file is configured correctly.');
}
}
// Start Server
app.listen(PORT, async () => {
console.log(`🚀 Server running on port ${PORT}`);
// Check database connection immediately after the server starts listening
await checkDatabaseConnection();
});