moved directory
This commit is contained in:
82
server.js
Normal file
82
server.js
Normal file
@@ -0,0 +1,82 @@
|
||||
// static variables
|
||||
const express = require("express");
|
||||
const app = express();
|
||||
const fs = require("fs");
|
||||
const path = require("path");
|
||||
const marked = require("marked");
|
||||
|
||||
const basePath = process.pkg ? path.dirname(process.execPath) : __dirname;
|
||||
|
||||
app.set("view engine", "ejs");
|
||||
|
||||
// static files
|
||||
app.use(express.static(path.join(basePath, "public")));
|
||||
|
||||
app.use(express.urlencoded({ extended: true }));
|
||||
app.use(express.json());
|
||||
|
||||
app.get("/add", (req, res) => {
|
||||
res.render("addRecipe/index");
|
||||
});
|
||||
|
||||
app.get("/", (req, res) => {
|
||||
// sync recipes
|
||||
const recipesFolder = path.join(basePath, "database", "recipes");
|
||||
|
||||
if (!fs.existsSync(recipesFolder)) {
|
||||
fs.mkdirSync(recipesFolder, { recursive: true });
|
||||
}
|
||||
|
||||
fs.readdir(recipesFolder, (err, files) => {
|
||||
if (err) {
|
||||
return res.status(500).send("Error by reading recipe files!");
|
||||
}
|
||||
|
||||
const txtFiles = files.filter((file) => file.endsWith(".txt"));
|
||||
|
||||
const recipes = txtFiles.map((file) => {
|
||||
const content = fs.readFileSync(path.join(recipesFolder, file), "utf8");
|
||||
return {
|
||||
title: path.basename(file, ".txt"),
|
||||
content: marked.parse(content),
|
||||
};
|
||||
});
|
||||
|
||||
res.render("index", { recipes });
|
||||
});
|
||||
});
|
||||
|
||||
// create recipe
|
||||
app.post("/add/create-recipe", (req, res) => {
|
||||
const { filename, content } = req.body;
|
||||
const recipesFolder = path.join(basePath, "database", "recipes");
|
||||
|
||||
if (!fs.existsSync(recipesFolder)) {
|
||||
fs.mkdirSync(recipesFolder, { recursive: true });
|
||||
}
|
||||
|
||||
const filePath = path.join(recipesFolder, `${filename}.txt`);
|
||||
|
||||
fs.writeFile(filePath, content, (err) => {
|
||||
if (err) {
|
||||
console.error(err);
|
||||
return res
|
||||
.status(500)
|
||||
.send("Error by creating file. Please check browser console!");
|
||||
}
|
||||
|
||||
res.render("success", {
|
||||
filename,
|
||||
path: filePath,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
// recipe router
|
||||
const recipeRouter = require("./routes/recipes");
|
||||
app.use("/recipe", recipeRouter);
|
||||
|
||||
// start server
|
||||
app.listen(8956, '0.0.0.0', () => {
|
||||
console.log("Server läuft auf Port 8956");
|
||||
});
|
Reference in New Issue
Block a user