|
|
@ -1,23 +1,52 @@ |
|
|
|
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'); |
|
|
|
|
|
|
|
let blog = []; |
|
|
|
//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()
|
|
|
|
res.send({ blog }); |
|
|
|
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 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); |
|
|
|
blog.push(blogeintrag); */ |
|
|
|
|
|
|
|
let blogeintrag = { |
|
|
|
id : blogInhalt.length + 1, |
|
|
|
title : req.body.title, |
|
|
|
username : req.body.username, |
|
|
|
date : req.body.date, |
|
|
|
text : req.body.text |
|
|
|
} |
|
|
|
|
|
|
|
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' }) |
|
|
|
}); |
|
|
|
|
|
|
@ -25,11 +54,17 @@ router.get('/newPost', function (req, res, next) { |
|
|
|
res.render('blogPost', { title: 'BlogEintrag' }); |
|
|
|
}); |
|
|
|
|
|
|
|
router.get('/:id', function (req, res, next) { |
|
|
|
let blogID = req.params.id; |
|
|
|
router.get('/:postID', function (req, res, next) { |
|
|
|
let blogID = req.params.postID; |
|
|
|
|
|
|
|
if(blogID > blogInhalt.length){ |
|
|
|
console.log("kein entsprechender Eintrag vorhanden"); |
|
|
|
res.send("kein entsprechender Eintrag vorhanden") |
|
|
|
} else { |
|
|
|
console.log(blogInhalt[blogID - 1]); |
|
|
|
res.send(blogInhalt[blogID - 1]); |
|
|
|
} |
|
|
|
|
|
|
|
console.log(blogID); |
|
|
|
res.send(blog[blogID]); |
|
|
|
}); |
|
|
|
|
|
|
|
module.exports = router; |