Make persistent storage location a constant

This commit is contained in:
powermaker450 2024-08-17 12:56:35 -04:00
parent 466dd184b1
commit d68d780951
3 changed files with 14 additions and 8 deletions

View file

@ -4,13 +4,11 @@ import http from "http";
import dotenv from "dotenv"; import dotenv from "dotenv";
import { Review, reviewSchema } from "./types"; import { Review, reviewSchema } from "./types";
import { checkFile } from "./utils"; import { checkFile } from "./utils";
import { dir } from "./utils";
dotenv.config(); dotenv.config();
const origin = chalk.bold(chalk.yellow("main.js: ")); const origin = chalk.bold(chalk.yellow("main.js: "));
const port = +process.env.PORT ?? 8080; const port = +process.env.PORT ?? 8080;
const contentType = { "Content-Type": "application/json" }; const contentType = { "Content-Type": "application/json" };
const fileName = dir + "data.json";
http http
.createServer((req, res) => { .createServer((req, res) => {
@ -37,7 +35,10 @@ http
); );
data.push(valid); data.push(valid);
fs.writeFileSync(fileName, JSON.stringify(data, null, 2)); fs.writeFileSync(
"./persist/data.json",
JSON.stringify(data, null, 2),
);
res.writeHead(200, contentType); res.writeHead(200, contentType);
res.write("OK"); res.write("OK");

View file

@ -13,15 +13,21 @@ export class Logger {
} }
log(text: any, args?: any): void { log(text: any, args?: any): void {
args ? console.log(this._main + text, args) : console.log(this._main + text); args
? console.log(this._main + text, args)
: console.log(this._main + text);
} }
warn(text: any, args?: any): void { warn(text: any, args?: any): void {
args ? console.warn(this._wrn + this._main + text, args) : console.warn(this._wrn + this._main + text); args
? console.warn(this._wrn + this._main + text, args)
: console.warn(this._wrn + this._main + text);
} }
error(text: any, args?: any): void { error(text: any, args?: any): void {
args ? console.error(this._err + this._main + text, args) : console.error(this._err + this._main + text); args
? console.error(this._err + this._main + text, args)
: console.error(this._err + this._main + text);
} }
} }

View file

@ -1,9 +1,8 @@
import fs from "fs"; import fs from "fs";
import { Review } from "../types"; import { Review } from "../types";
export const dir = "./persist/";
export function checkFile(file: string, encoding: fs.EncodingOption): Review[] { export function checkFile(file: string, encoding: fs.EncodingOption): Review[] {
const dir = "./persist/";
const fullPath = dir + file; const fullPath = dir + file;
let final: Review[]; let final: Review[];