mnglab
Version:
Dead simple REST micro-framework with modular routing, API key, rate limiting, full HTTP response handling, dev tools, JSON DB, file helpers, endpoint stats, and professional developer experience.
53 lines (45 loc) • 1.14 kB
JavaScript
const fs = require('fs');
const path = require('path');
function read(filePath, encoding = 'utf-8') {
const fullPath = path.resolve(process.cwd(), filePath);
if (!fs.existsSync(fullPath)) return null;
return fs.readFileSync(fullPath, encoding);
}
function write(filePath, content, encoding = 'utf-8') {
const fullPath = path.resolve(process.cwd(), filePath);
fs.writeFileSync(fullPath, content, { encoding });
return true;
}
function readJSON(filePath) {
try {
const content = read(filePath);
return content ? JSON.parse(content) : null;
} catch (e) {
return null;
}
}
function writeJSON(filePath, obj) {
try {
write(filePath, JSON.stringify(obj, null, 2));
return true;
} catch (e) {
return false;
}
}
function listDir(dirPath) {
const fullPath = path.resolve(process.cwd(), dirPath);
if (!fs.existsSync(fullPath)) return [];
return fs.readdirSync(fullPath);
}
function fileExists(filePath) {
const fullPath = path.resolve(process.cwd(), filePath);
return fs.existsSync(fullPath);
}
module.exports = {
read,
write,
readJSON,
writeJSON,
listDir,
fileExists
};