forked from deadvey/blogger-nodejs
Seperated all routes into seperate files for neatness, and I've made the comments.json store only the comments, comments_counter is in the new data.json file which also stores the hitcount. Signed-off-by: max <deadvey@localhost.localdomain>
61 lines
1.4 KiB
JavaScript
61 lines
1.4 KiB
JavaScript
import { createRequire } from "module";
|
|
const require = createRequire(import.meta.url);
|
|
|
|
const config = require("../config.json")
|
|
const fs = require("fs")
|
|
|
|
export function getdata(data, key='', value='') {
|
|
|
|
if (config["data_storage"] == "json") {
|
|
if (data == "posts" || data == 'users' || data == 'comments') {
|
|
let result = require(`../data/${data}.json`)
|
|
if (key != '') {
|
|
result.forEach((object, index) => {
|
|
if (object[key] == value) {
|
|
return object
|
|
}
|
|
})
|
|
}
|
|
return result
|
|
}
|
|
else if (data == "hitcount") {
|
|
let result = fs.readFileSync("../data/hitcount.txt")
|
|
return result
|
|
}
|
|
else {
|
|
console.log("Error, invalid requested")
|
|
return 1
|
|
}
|
|
}
|
|
|
|
if (config["data_storage"] == "mysql") {
|
|
const mysql = require('mysql');
|
|
let con = mysql.createConnection({
|
|
host: config.database.host,
|
|
user: config.database.user,
|
|
password: config.database.password,
|
|
database: config.database.database,
|
|
});
|
|
|
|
con.connect(function(err) {
|
|
if (err) throw err;
|
|
|
|
if (data == "posts" || data == 'users' || data == 'comments') {
|
|
con.query(`SELECT * FROM ${data}`, function (err, result, fields) {
|
|
if (err) throw err;
|
|
console.log(result)
|
|
return result;
|
|
});
|
|
}
|
|
else if (data == 'hitcount') {
|
|
con.query(`SELECT paramValue FROM params WHERE paramName = '${data}'`, function (err, result, fields) {
|
|
if (err) throw err;
|
|
console.log(result)
|
|
return result;
|
|
});
|
|
|
|
}
|
|
});
|
|
}
|
|
}
|