Skip to content

Instantly share code, notes, and snippets.

@yebt
Last active October 20, 2022 19:23
Show Gist options
  • Save yebt/db9264c5d102a68d2eaf6c58417f4d04 to your computer and use it in GitHub Desktop.
Save yebt/db9264c5d102a68d2eaf6c58417f4d04 to your computer and use it in GitHub Desktop.
# use it like adebig api server
const express = require("express");
const fs = require("fs");
const fsp = require("fs/promises");
const cors = require("cors");
const app = express();
const hostname = "127.0.0.1";
//const port = 3000; // 80
const port = 80; // 80
const filePathName = "./mmsg.json";
app.use(express.json());
app.use(cors());
app.get("/", async (req, res) => {
let filehandle = null;
try {
filehandle = await fsp.open(filePathName, 'a+');
const data = await filehandle.readFile({ encoding: 'utf8' });
const jsonData = JSON.parse(data ? data : JSON.stringify(data));
res.json(jsonData ? jsonData : { 'message': 'Sorry, No data' });
} catch (error) {
console.log(error);
res.json({ sorry: 'Something went wrong :C. try later' });
} finally {
if (filehandle) await filehandle.close();
}
});
app.get("/hope", (req, res) => {
res.json({ message: "Hope node debug server" });
});
app.post("/", async (req, res) => {
let filehandle = null;
let filehandleW = null;
try {
let date = new Date();
// show date
let message = {
"date": date.toLocaleString('col', { timeZone: 'America/Bogota' }),
"body": req.body
};
filehandle = await fsp.open(filePathName, 'a+');
const oldData = await filehandle.readFile({ encoding: 'utf8' });
let jsonData = JSON.parse(oldData ? oldData : '[]');
jsonData.unshift(message);
console.log(oldData)
console.log(jsonData)
console.log(JSON.stringify(jsonData))
filehandleW = await fsp.open(filePathName, 'w+');
await filehandleW.writeFile(JSON.stringify(jsonData), { flag: 'w+', encoding: 'utf8' });
} catch (error) {
console.log(error);
res.json({ sorry: 'something went wrong while trying to save :C. try later' });
} finally {
if (filehandle) await filehandle.close();
}
res.json({ message: 'message Saved' })
});
app.post("/save", async (req, res) => {
let filehandle = null;
let filehandleW = null;
try {
let date = new Date();
// show date
let message = {
"date": date.toLocaleString('col', { timeZone: 'America/Bogota' }),
"body": req.body
};
filehandle = await fsp.open(filePathName, 'a+');
const oldData = await filehandle.readFile({ encoding: 'utf8' });
let jsonData = JSON.parse(oldData ? oldData : '[]');
jsonData.unshift(message);
console.log(oldData)
console.log(jsonData)
console.log(JSON.stringify(jsonData))
filehandleW = await fsp.open(filePathName, 'w+');
await filehandleW.writeFile(JSON.stringify(jsonData), { flag: 'w+', encoding: 'utf8' });
} catch (error) {
console.log(error);
res.json({ sorry: 'something went wrong while trying to save :C. try later' });
} finally {
if (filehandle) await filehandle.close();
}
res.json({ message: 'message Saved' })
});
app.get("/all", async (req, res) => {
let filehandle = null;
try {
filehandle = await fsp.open(filePathName, 'a+');
const data = await filehandle.readFile({ encoding: 'utf8' });
const jsonData = JSON.parse(data ? data : JSON.stringify(data));
res.json(jsonData ? jsonData : { 'message': 'Sorry, No data' });
} catch (error) {
console.log(error);
res.json({ sorry: 'Something went wrong :C. try later' });
} finally {
if (filehandle) await filehandle.close();
}
});
app.get("/clear", async (req, res) => {
let filehandleW = null;
try {
filehandleW = await fsp.open(filePathName, 'w+');
await filehandleW.writeFile("", { flag: 'w+', encoding: 'utf8' });
res.json({ "message": "Cleared UwU" });
} catch (error) {
console.log(error);
res.json({ sorry: 'Something went wrong :C. try later' });
} finally {
if (filehandleW) await filehandleW.close();
}
});
app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
});
{
"name": "nodedebugserver",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"dev": "nodemon -L index.cjs",
"start": "nodemon -L index.cjs"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.1",
"nodemon": "^2.0.20"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment