I have the followin node js app that works perfectly locally, but not in the server. To be more specific it's deployed in a subdomain and in a subfolder of httpdocs.
require("dotenv").config();
const express = require("express");
const mysql = require("mysql2");
const mqtt = require("mqtt");
const path = require("path");
const bodyParser = require("body-parser");
// Define the MQTT broker options
const options = {
port: process.env.MQTT_PORT,
username: process.env.MQTT_USERNAME,
password: process.env.MQTT_PASSWORD,
clientId: `mqttjs_${Math.random().toString(16).substr(2, 8)}`,
protocol: "wss",
};
// Define the MQTT broker address and topic
const broker = process.env.MQTT_BROKER;
const topic = process.env.MQTT_TOPIC;
// Connect to the MQTT broker
const client = mqtt.connect(broker, options);
// Initialize to 4 minutes ago Τελευταία εισαγωγή στη βάση
let lastInsertTime = Date.now() - 4 * 60 * 1000;
// Initialize the last received message
let lastMessage = null;
// Initialize countdown
let countdown = 4 * 60; // 3 minutes in seconds
// Initialize message and DB insert counts
let messageCount = 0;
let dbInsertCount = 0;
// Set up a new MySQL connection
const connection = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
});
connection.connect(function (err) {
if (err) {
console.error("Failed to connect to the database:", err);
process.exit(1);
}
});
client.on("connect", function () {
client.subscribe(topic, function (err) {
if (!err) {
console.log("Successfully subscribed to the topic");
}
});
});
let lastReceivedMessageTime = Date.now();
let noData = false;
let countdownInterval = setInterval(() => {
if (countdown > 0 && !noData) {
countdown--;
const minutes = Math.floor(countdown / 60);
const seconds = countdown % 60;
console.log(
`Time remaining until next insert: ${minutes} minutes and ${seconds} seconds`
);
} else {
clearInterval(countdownInterval);
}
}, 1000);
client.on("message", function (topic, message) {
lastReceivedMessageTime = Date.now();
noData = false;
messageCount++;
lastMessage = message;
console.log("Received message:", message.toString());
// Reset the countdown only if needed
if (Date.now() - lastInsertTime >= 4 * 60 * 1000 && lastMessage) {
countdown = 4 * 60; // Reset the countdown
const data = JSON.parse(lastMessage.toString());
let sql = "INSERT INTO `vivus02` SET ?";
let dataObj = {};
data.d.forEach((item) => {
dataObj[item.tag] = item.value;
});
dataObj.timestamp = data.ts;
connection.query(sql, dataObj, function (error, results, fields) {
if (error) {
console.error("Failed to insert data into the database:", error);
return;
}
console.log("Data successfully inserted into the database");
dbInsertCount++;
lastInsertTime = Date.now();
lastMessage = null;
});
}
});
// // Start the countdown
// let countdownInterval = setInterval(() => {
// if (countdown > 0) {
// countdown--;
// }
// if (noData) {
// clearInterval(countdownInterval); // Stop the countdown if no data is available
// } else {
// // Calculate minutes and seconds
// const minutes = Math.floor(countdown / 60);
// const seconds = countdown % 60;
// // Print the countdown
// console.log(
// `Time remaining until next insert: ${minutes} minutes and ${seconds} seconds`
// );
// }
// }, 10000);
process.on("exit", (code) => {
console.log(`About to exit with code: ${code}`);
});
// Set up Express.js server
const app = express();
// Serve static files from the current directory
app.use(express.static(__dirname));
console.log(__dirname);
// Use body-parser middleware to parse JSON requests
app.use(bodyParser.json());
app.get("/", (req, res) => {
res.sendFile(path.join(__dirname + "/index.html"));
});
setInterval(() => {
if (Date.now() - lastReceivedMessageTime > 2 * 60 * 1000) {
// No data if no messages in the last 2 minutes
noData = true;
}
}, 2 * 60 * 1000);
app.get("/data", (req, res) => {
res.json({
messageCount,
dbInsertCount,
tableName: "vivus02",
mqttTopic: topic,
nextInsertTime: lastInsertTime + 4 * 60 * 1000, // Send the time of the next insert operation
noData,
});
});
/// Use body-parser middleware to parse JSON requests
app.use(bodyParser.json());
// Define a route to handle table creation requests
app.post("/createTable", (req, res) => {
// Extract table name and JSON string from the request body
const { table, jsonString } = req.body;
try {
// Parse the JSON string into a JavaScript object
const jsonData = JSON.parse(jsonString);
// Extract data array from JSON object
const data = jsonData.d;
// Extract tags and their corresponding value types from the data array
const columns = data.map((item) => {
const value = item.value;
const valueType = typeof value;
let dataType;
switch (valueType) {
case "string":
dataType = "VARCHAR(255)";
break;
case "number":
if (Number.isInteger(value)) {
dataType = "INT";
} else {
dataType = "DECIMAL(10, 2)";
}
break;
case "boolean":
dataType = "BOOLEAN";
break;
default:
dataType = "VARCHAR(255)";
}
return { tag: item.tag, dataType: dataType };
});
// Connect to the MySQL database
const connection = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_NAME,
});
// Define the SQL query to create the table
let createTableQuery = `CREATE TABLE IF NOT EXISTS ${table} (
id INT AUTO_INCREMENT PRIMARY KEY,
ts DATETIME`;
// Loop through the columns and add them as columns to the table
columns.forEach((column) => {
createTableQuery += `,
${column.tag} ${column.dataType}`;
});
// Close the SQL query
createTableQuery += ")";
// Execute the SQL query
connection.query(createTableQuery, (error, results, fields) => {
if (error) {
// If an error occurs, send an error response
console.error("Failed to create table:", error);
res
.status(500)
.json({ success: false, message: "Failed to create table" });
} else {
// If the table is created successfully, send a success response
console.log("Table created successfully");
res
.status(200)
.json({ success: true, message: "Table created successfully" });
}
// Close the MySQL connection
connection.end();
});
} catch (error) {
// If an error occurs during JSON parsing, send an error response
console.error("Failed to parse JSON string:", error);
res.status(400).json({ success: false, message: "Invalid JSON string" });
}
});
//CREATE TABLE END
app.listen(3000, () => {
console.log("Server is running on port 3000");
});
I really don't know what to do... I've tried everything
I think that something has to do with the routes/paths of the subdomain. In nodejs logs I don't get any errors.