wf-radio/server.js
Gu://em_ 306c87fca2 feat: Error handling middleware
feat: Ability to get the db variable from everywhere with getDatabase function
feat: Hello world when getting root url
fix: Now mods model is able to communicate with db
fix: fixed a syntax error in routes/mods.js file which caused sending two responses to the client
2025-03-29 00:31:25 +01:00

54 lines
1.2 KiB
JavaScript

// --- Imports ---
const express = require("express");
const app = express();
const fs = require("fs");
const path = require("path");
const { getDatabase, connectDatabase } = require('./src/database/index');
const handleError = require('./src/middleware/errors');
// --- Define constants ---
const config_folder = "config";
const config_file_name = "config.json"
const data_folder = "data";
const sqlite_file_name = "sqlite.db"
const default_port = 8000
// --- Load configuration ---
// var declaration
let db;
let config;
// Load config
try {
config = JSON.parse(fs.readFileSync(path.join(config_folder, config_file_name)));
console.debug("Loaded config");
} catch (err) {
console.error("Couldn't read config file: ", err);
process.exit(1);
}
// vars definition
const port = config.port || default_port;
console.debug("Port: ", port);
// Database connection
db = connectDatabase(config.database);
// --- Routing ---
app.use("/", require("./src/routes/root"));
app.use("/mods", require("./src/routes/mods"));
// -- Error handling ---
app.use(handleError);
// --- Launch ---
app.listen(port, () => {
console.log("Server listening on port " + port + "...");
})