Update server.js
Browse files
server.js
CHANGED
|
@@ -1,64 +1,47 @@
|
|
| 1 |
const express = require('express');
|
| 2 |
const session = require('express-session');
|
|
|
|
|
|
|
| 3 |
const authRoutes = require('./routes/auth');
|
| 4 |
-
const userRoutes = require('./routes/user');
|
| 5 |
const adminRoutes = require('./routes/admin');
|
|
|
|
| 6 |
const investmentRoutes = require('./routes/investment');
|
| 7 |
-
const cron = require('./cron/roiCron');
|
| 8 |
-
const path = require('path');
|
| 9 |
-
const fs = require('fs');
|
| 10 |
-
const multer = require('multer');
|
| 11 |
-
require('dotenv').config();
|
| 12 |
|
| 13 |
const app = express();
|
| 14 |
|
| 15 |
-
//
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
fs.mkdirSync(uploadDir, { recursive: true });
|
| 19 |
-
}
|
| 20 |
-
|
| 21 |
-
// Configure multer for file uploads
|
| 22 |
-
const storage = multer.diskStorage({
|
| 23 |
-
destination: (req, file, cb) => {
|
| 24 |
-
cb(null, uploadDir);
|
| 25 |
-
},
|
| 26 |
-
filename: (req, file, cb) => {
|
| 27 |
-
cb(null, `${Date.now()}-${file.originalname}`);
|
| 28 |
-
}
|
| 29 |
-
});
|
| 30 |
-
const upload = multer({
|
| 31 |
-
storage,
|
| 32 |
-
fileFilter: (req, file, cb) => {
|
| 33 |
-
const allowedTypes = ['image/jpeg', 'image/png', 'application/pdf'];
|
| 34 |
-
if (allowedTypes.includes(file.mimetype)) {
|
| 35 |
-
cb(null, true);
|
| 36 |
-
} else {
|
| 37 |
-
cb(new Error('Invalid file type. Only JPG, PNG, PDF allowed.'));
|
| 38 |
-
}
|
| 39 |
-
},
|
| 40 |
-
limits: { fileSize: 5 * 1024 * 1024 } // 5MB limit
|
| 41 |
-
});
|
| 42 |
|
| 43 |
-
|
| 44 |
app.use(express.urlencoded({ extended: true }));
|
|
|
|
| 45 |
app.use(session({
|
| 46 |
-
secret: process.env.SESSION_SECRET || 'your-
|
| 47 |
resave: false,
|
| 48 |
saveUninitialized: false
|
| 49 |
}));
|
| 50 |
-
app.set('view engine', 'ejs');
|
| 51 |
-
app.set('views', path.join(__dirname, 'public/views'));
|
| 52 |
-
app.use(express.static(path.join(__dirname, 'public')));
|
| 53 |
-
app.use('/kyc_uploads', express.static(uploadDir));
|
| 54 |
|
| 55 |
-
//
|
| 56 |
-
|
| 57 |
-
req
|
| 58 |
-
|
| 59 |
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
|
| 61 |
-
// Serve KYC documents
|
| 62 |
app.get('/admin/kyc-document/:id', async (req, res) => {
|
| 63 |
try {
|
| 64 |
const [doc] = await require('./config/db').query('SELECT file_path FROM kyc_documents WHERE id = ?', [req.params.id]);
|
|
@@ -69,12 +52,11 @@ app.get('/admin/kyc-document/:id', async (req, res) => {
|
|
| 69 |
}
|
| 70 |
});
|
| 71 |
|
| 72 |
-
|
| 73 |
-
app.use(
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
app.get('/', (req, res) => res.render('index'));
|
| 78 |
|
| 79 |
const PORT = process.env.PORT || 3000;
|
| 80 |
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
|
|
|
|
| 1 |
const express = require('express');
|
| 2 |
const session = require('express-session');
|
| 3 |
+
const path = require('path');
|
| 4 |
+
const multer = require('multer');
|
| 5 |
const authRoutes = require('./routes/auth');
|
|
|
|
| 6 |
const adminRoutes = require('./routes/admin');
|
| 7 |
+
const userRoutes = require('./routes/user');
|
| 8 |
const investmentRoutes = require('./routes/investment');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
const app = express();
|
| 11 |
|
| 12 |
+
// Set view engine
|
| 13 |
+
app.set('view engine', 'ejs');
|
| 14 |
+
app.set('views', path.join(__dirname, 'public/views'));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
// Middleware
|
| 17 |
app.use(express.urlencoded({ extended: true }));
|
| 18 |
+
app.use(express.static(path.join(__dirname, 'public')));
|
| 19 |
app.use(session({
|
| 20 |
+
secret: process.env.SESSION_SECRET || 'your-session-secret',
|
| 21 |
resave: false,
|
| 22 |
saveUninitialized: false
|
| 23 |
}));
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
|
| 25 |
+
// Multer setup for file uploads
|
| 26 |
+
const storage = multer.diskStorage({
|
| 27 |
+
destination: (req, file, cb) => cb(null, 'kyc_uploads/'),
|
| 28 |
+
filename: (req, file, cb) => cb(null, `${Date.now()}-${file.originalname}`)
|
| 29 |
});
|
| 30 |
+
const upload = multer({ storage });
|
| 31 |
+
app.use('/user/kyc', upload.fields([
|
| 32 |
+
{ name: 'id_doc', maxCount: 1 },
|
| 33 |
+
{ name: 'utility_doc', maxCount: 1 },
|
| 34 |
+
{ name: 'selfie_doc', maxCount: 1 },
|
| 35 |
+
{ name: 'ssn_doc', maxCount: 1 }
|
| 36 |
+
]));
|
| 37 |
+
|
| 38 |
+
// Routes
|
| 39 |
+
app.use('/', authRoutes);
|
| 40 |
+
app.use('/admin', adminRoutes);
|
| 41 |
+
app.use('/user', userRoutes);
|
| 42 |
+
app.use('/investment', investmentRoutes);
|
| 43 |
|
| 44 |
+
// Serve KYC documents (admin access only)
|
| 45 |
app.get('/admin/kyc-document/:id', async (req, res) => {
|
| 46 |
try {
|
| 47 |
const [doc] = await require('./config/db').query('SELECT file_path FROM kyc_documents WHERE id = ?', [req.params.id]);
|
|
|
|
| 52 |
}
|
| 53 |
});
|
| 54 |
|
| 55 |
+
// Error handling
|
| 56 |
+
app.use((err, req, res, next) => {
|
| 57 |
+
console.error(err.stack);
|
| 58 |
+
res.status(500).send('Something went wrong!');
|
| 59 |
+
});
|
|
|
|
| 60 |
|
| 61 |
const PORT = process.env.PORT || 3000;
|
| 62 |
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
|