The backend and especially server.js will grow pretty big from adding functionality. This is somewhat psuedo coded as a example here but here's the proposed structure...
/src/
server.js
app.js
config/
db.js
cors.js
routes/
authRoutes.js
adminRoutes.js
streamRoutes.js
healthRoutes.js
controllers/
AuthControllers.js
StreamController.js
middleware/
auth.js
utils/
paths.js
server.js will be somewhat similar. But all it does is start the server.
import 'dotenv/config';
import mongoose from 'mongoose';
import app from './app.js';
const PORT = process.env.PORT || 3000;
const MONGODB_URI = process.env.MONGODB_URI;
// Connect DB
if (MONGODB_URI) {
mongoose
.connect(MONGODB_URI)
.then(() => console.log('🟢 MongoDB connected'))
.catch(err => console.error('🔴 MongoDB error:', err));
} else {
console.log('⚠️ No MONGODB_URI provided — running in mock mode.');
}
// Start server
app.listen(PORT, () => {
console.log(`\n🚀 Server running at http://localhost:${PORT}`);
});
Then we have a Express app called app.js that uses app method from express. Routes done by Express router method in individual files:
import express from 'express';
import cors from 'cors';
import morgan from 'morgan';
import { corsOptions } from './config/cors.js';
import authRoutes from './routes/authRoutes.js';
import adminRoutes from './routes/adminRoutes.js';
import streamRoutes from './routes/streamRoutes.js';
import healthRoutes from './routes/healthRoutes.js';
const app = express();
// Middleware
app.use(morgan('dev'));
app.use(cors(corsOptions));
app.use(express.json());
// Routes
app.use('/api/auth', authRoutes);
app.use('/api/admin', adminRoutes);
app.use('/api/streams', streamRoutes);
app.use('/health', healthRoutes);
// Root route
app.get('/', (req, res) => {
res.send(`
<div style="font-family: sans-serif; text-align: center; padding-top: 50px;">
<h1>DevStream API is Online</h1>
<p>The server is running correctly.</p>
<p>Access your data here: <a href="/api/streams">/api/streams</a></p>
</div>
`);
});
// 404 handler
app.use((req, res) => {
res.status(404).json({ error: 'Route not found' });
});
export default app;
Create the expanded server.js
The backend and especially server.js will grow pretty big from adding functionality. This is somewhat psuedo coded as a example here but here's the proposed structure...
server.js will be somewhat similar. But all it does is start the server.
Then we have a Express app called app.js that uses
appmethod from express. Routes done by Express router method in individual files:Create the expanded server.js