You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
2.5 KiB
96 lines
2.5 KiB
var express = require('express');
|
|
let res = require('express/lib/response');
|
|
var router = express.Router();
|
|
//fileStream ermöglicht Lesen und Schreiben auf Festplatte
|
|
var fs = require('fs');
|
|
const { ServerResponse } = require('http');
|
|
|
|
//let blog = [];
|
|
|
|
//erstellt JSONObjekt
|
|
let rawdata = fs.readFileSync('./public/models/blog.json');
|
|
//wandelt JSONObjekt in JsObjekt
|
|
let blogInhalt = JSON.parse(rawdata);
|
|
|
|
/* GET home page. */
|
|
router.route('/')
|
|
.get((req, res, next) => { // => Pfeilfunktion: ersetzt function()
|
|
for (i = 0; i < blogInhalt.length; i++) {
|
|
console.log(blogInhalt[i].id, ".", blogInhalt[i].title);
|
|
}
|
|
res.send({ blogInhalt });
|
|
})
|
|
.post((req, res, next) => {
|
|
|
|
/*let blogeintrag = [];
|
|
let title = req.body.title;
|
|
let username = req.body.username;
|
|
let date = req.body.date;
|
|
let text = req.body.text;
|
|
console.log(title);
|
|
blogeintrag.push(title, username, date, text);
|
|
blog.push(blogeintrag); */
|
|
let blogID;
|
|
|
|
if (blogInhalt.length == 0) {
|
|
blogID = 1;
|
|
} else {
|
|
blogID = (blogInhalt[blogInhalt.length - 1].id) + 1;
|
|
}
|
|
|
|
try {
|
|
if (!req.body.file) {
|
|
res.send({
|
|
status: false,
|
|
message: "No file received"
|
|
});
|
|
} else {
|
|
let file = req.body.file;
|
|
file.mv('./uploads/' + file.name);
|
|
res.send('freude...' + file.name);
|
|
console.log("Datei empfangen");
|
|
}
|
|
} catch (err) {
|
|
res.status(500).send(err);
|
|
}
|
|
|
|
let blogeintrag = {
|
|
id: blogID,
|
|
title: req.body.title,
|
|
username: req.body.username,
|
|
date: req.body.date,
|
|
text: req.body.text,
|
|
file: req.body.file
|
|
}
|
|
|
|
console.log(blogeintrag);
|
|
|
|
//hänge blogeintrag an blogInhalt
|
|
blogInhalt.push(blogeintrag);
|
|
|
|
let date = JSON.stringify(blogInhalt, null, 2);
|
|
|
|
fs.writeFileSync('./public/models/blog.json', date);
|
|
|
|
res.render('blogPost', { title: 'Nächster Post' })
|
|
});
|
|
|
|
router.get('/newPost', function (req, res, next) {
|
|
res.render('blogPost', { title: 'BlogEintrag' });
|
|
});
|
|
|
|
router.get('/:postID', function (req, res, next) {
|
|
let blogID = req.params.postID;
|
|
|
|
for (i = 0; i < blogInhalt.length; i++) {
|
|
if (blogInhalt[i].id == blogID) {
|
|
console.log(blogInhalt[i].id, blogInhalt[i].title);
|
|
res.send({ ID: blogInhalt[i].id, Titel: blogInhalt[i].title });
|
|
}
|
|
}
|
|
res.send({ "text: ": "Kein entsprechender Eintrag gefunden!" })
|
|
console.log('Kein entsprechender Eintrag gefunden.');
|
|
|
|
});
|
|
|
|
module.exports = router;
|