Files
DRB-Client/express/server.mjs

60 lines
1.6 KiB
JavaScript

import express from 'express';
import http from 'http';
import path from 'path';
import { fileURLToPath } from 'url';
import bodyParser from 'body-parser';
import setupRoutes from './routes/setupRoutes.js';
import dashboardRoutes from './routes/dashboardRoutes.js';
const __dirname = path.dirname(fileURLToPath(import.meta.url)); // Define __dirname for ESM
export var isSetupComplete = false;
const app = express();
const server = http.createServer(app);
// Start the server
const startServer = (port, openSocket) => {
if (openSocket) isSetupComplete = true;
server.listen(port, () => {
console.log(`Server running on port ${port}`);
});
};
export default startServer;
// Set EJS as the default template engine
app.set('view engine', 'ejs');
// Set the views directory to express/views
const viewsPath = path.join(__dirname, 'views');
app.set('views', viewsPath);
// Use body-parser middleware to parse JSON requests
app.use(bodyParser.json());
// Use body-parser middleware to parse URL-encoded form data
app.use(bodyParser.urlencoded({ extended: true }));
// Define static folder for serving HTML, CSS, and client-side JavaScript files
const staticPath = path.join(__dirname, 'public');
app.use(express.static(staticPath));
// ---------------- Defualt Route
app.get('/', (req, res) => {
if (!isSetupComplete) {
return res.redirect('/setup');
}
else {
return res.redirect('/dashboard');
}
});
// ---------------- Routers
// Use the setup router for '/setup' routes
app.use('/setup', setupRoutes);
// Use the dashboard router for '/dashboard' routes
app.use('/dashboard', dashboardRoutes);