Browse Source

HA05: Controller bearbeitet und log-Datei erstellt

master
lauraF 3 years ago
parent
commit
d2c06b488e
  1. 63
      access.log
  2. 4
      app.js
  3. 40
      controllers/blogController.js
  4. 24
      public/models/blog.json
  5. 88
      routes/blog.js

63
access.log

@ -0,0 +1,63 @@
::1 - - [16/May/2022:14:36:33 +0000] "POST /blog HTTP/1.1" 200 4090 "http://localhost/blog/newPost" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:36:33 +0000] "GET /stylesheets/bootstrap.min.css HTTP/1.1" 304 - "http://localhost/blog" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:36:33 +0000] "GET /javascripts/bootstrap.bundle.min.js HTTP/1.1" 304 - "http://localhost/blog" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:36:33 +0000] "GET /stylesheets/style.css HTTP/1.1" 304 - "http://localhost/blog" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:36:33 +0000] "GET /images/logo.PNG HTTP/1.1" 304 - "http://localhost/blog" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:36:36 +0000] "POST /blog HTTP/1.1" 200 4090 "http://localhost/blog" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:36:36 +0000] "GET /stylesheets/bootstrap.min.css HTTP/1.1" 304 - "http://localhost/blog" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:36:36 +0000] "GET /javascripts/bootstrap.bundle.min.js HTTP/1.1" 304 - "http://localhost/blog" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:36:36 +0000] "GET /stylesheets/style.css HTTP/1.1" 304 - "http://localhost/blog" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:36:36 +0000] "GET /images/logo.PNG HTTP/1.1" 304 - "http://localhost/blog" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:36:38 +0000] "GET /blog HTTP/1.1" 200 286 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:36:47 +0000] "GET /blog/:3 HTTP/1.1" - - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:36:50 +0000] "GET /blog/3 HTTP/1.1" - - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:40:55 +0000] "GET /blog/:3 HTTP/1.1" - - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:40:57 +0000] "GET /blog/:3 HTTP/1.1" - - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:41:00 +0000] "GET /blog/3 HTTP/1.1" - - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::ffff:127.0.0.1 - - [16/May/2022:14:43:19 +0000] "GET /blog/:3 HTTP/1.1" 500 1283 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:43:26 +0000] "GET /blog/ HTTP/1.1" 200 286 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:43:29 +0000] "GET /blog/:3 HTTP/1.1" 500 1283 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:43:30 +0000] "GET /blog/:3 HTTP/1.1" 500 1283 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:44:13 +0000] "GET /blog/:3 HTTP/1.1" 200 16 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:44:17 +0000] "GET /blog/:2 HTTP/1.1" 200 16 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:44:20 +0000] "GET /blog/:1 HTTP/1.1" 200 16 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:44:57 +0000] "GET /blog/:1 HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:45:20 +0000] "GET /blog/ HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:47:43 +0000] "GET /blog/ HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:47:47 +0000] "GET /blog/:3 HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:47:50 +0000] "GET /blog/:2 HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:48:07 +0000] "GET /blog/:2 HTTP/1.1" 200 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:48:10 +0000] "GET /blog/:1 HTTP/1.1" 200 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:48:10 +0000] "GET /blog/:1 HTTP/1.1" 200 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:48:24 +0000] "GET /blog/:1 HTTP/1.1" 200 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:49:17 +0000] "GET /blog/:1 HTTP/1.1" 200 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:49:20 +0000] "GET /blog/:2 HTTP/1.1" 200 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:52:46 +0000] "GET /blog/:2 HTTP/1.1" 200 58 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:53:13 +0000] "GET /blog/:2 HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:53:15 +0000] "GET /blog/:1 HTTP/1.1" 200 58 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:53:18 +0000] "GET /blog/:3 HTTP/1.1" 200 58 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:53:18 +0000] "GET /blog/:3 HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:53:32 +0000] "GET /blog/:3 HTTP/1.1" 200 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:53:52 +0000] "GET /blog/:3 HTTP/1.1" 200 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:54:07 +0000] "GET /blog/:3 HTTP/1.1" 200 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:54:14 +0000] "GET /blog/3 HTTP/1.1" 200 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:54:14 +0000] "GET /blog/3 HTTP/1.1" 200 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:54:27 +0000] "GET /blog/2 HTTP/1.1" 200 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:55:51 +0000] "GET /blog/newPost HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:55:51 +0000] "GET /stylesheets/bootstrap.min.css HTTP/1.1" 304 - "http://localhost/blog/newPost" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:55:51 +0000] "GET /javascripts/bootstrap.bundle.min.js HTTP/1.1" 304 - "http://localhost/blog/newPost" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:55:51 +0000] "GET /stylesheets/style.css HTTP/1.1" 304 - "http://localhost/blog/newPost" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:55:51 +0000] "GET /images/logo.PNG HTTP/1.1" 304 - "http://localhost/blog/newPost" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:55:54 +0000] "GET /blog/newPost HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:55:54 +0000] "GET /javascripts/bootstrap.bundle.min.js HTTP/1.1" 304 - "http://localhost/blog/newPost" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:55:54 +0000] "GET /images/logo.PNG HTTP/1.1" 304 - "http://localhost/blog/newPost" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:56:31 +0000] "GET /blog/newPost HTTP/1.1" 304 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:56:31 +0000] "GET /stylesheets/bootstrap.min.css HTTP/1.1" 304 - "http://localhost/blog/newPost" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:56:31 +0000] "GET /javascripts/bootstrap.bundle.min.js HTTP/1.1" 304 - "http://localhost/blog/newPost" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:56:31 +0000] "GET /stylesheets/style.css HTTP/1.1" 304 - "http://localhost/blog/newPost" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:56:31 +0000] "GET /images/logo.PNG HTTP/1.1" 304 - "http://localhost/blog/newPost" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:56:33 +0000] "GET /blog/3 HTTP/1.1" 200 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:57:21 +0000] "GET /blog/3 HTTP/1.1" 200 70 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:57:24 +0000] "GET /blog/2 HTTP/1.1" 200 58 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:57:27 +0000] "GET /blog/1 HTTP/1.1" 200 68 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"
::1 - - [16/May/2022:14:57:29 +0000] "GET /blog/5 HTTP/1.1" 200 - "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 OPR/86.0.4363.59"

4
app.js

@ -2,6 +2,7 @@ var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var fs = require('fs');
var logger = require('morgan');
var indexRouter = require('./routes/index');
@ -10,11 +11,14 @@ var blogRouter = require('./routes/blog');
var app = express();
//Log to File
var accessLogStream = fs.createWriteStream(path.join('./', 'access.log'), {flags: 'a'});
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(logger('dev'));
app.use(logger('combined', {stream: accessLogStream}));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());

40
controllers/blogController.js

@ -12,37 +12,33 @@ var getAllPosts=(req, res) => {
}
var createPost = (req, res) => {
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);
let blogeintrag = {
id : blogInhalt.length?blogInhalt[blogInhalt.length - 1].id+1 : 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);
//hänge blogeintrag an blogInhalt
blogInhalt.push(blogeintrag);
let date = JSON.stringify(blogInhalt, null, 2);
let date = JSON.stringify(blogInhalt, null, 2);
fs.writeFileSync('./public/models/blog.json', date);
fs.writeFileSync('./public/models/blog.json', date);
res.render('blogPost', { title: 'Nächster Post' })
res.render('blogPost', { title: 'Nächster Post' })
}
var getPost = (req, res) => {
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]);
}
let blogID = parseInt(req.params.postID);
console.log(blogID);
let found = blogInhalt.find(element => element.id === blogID);
found?res.send(found):res.send('nichts gefunden');
}
module.exports = {

24
public/models/blog.json

@ -1,18 +1,30 @@
[
{
"id": 1,
"title": "titel",
"title": "Titel 3",
"username": "name",
"date": "",
"text": "text",
"file": ""
"text": "Text"
},
{
"id": 2,
"title": "titel",
"title": "4",
"username": "",
"date": "",
"text": "",
"file": ""
"text": "rrde"
},
{
"id": 3,
"title": "titel eines blogs",
"username": "",
"date": "",
"text": ""
},
{
"id": 4,
"title": "und noch ein Blog",
"username": "",
"date": "",
"text": ""
}
]

88
routes/blog.js

@ -1,96 +1,22 @@
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);
var blogControllers = require('../controllers/blogController');
/* 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' })
});
.get(blogControllers.getAllPosts)
.post(blogControllers.createPost);
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.');
router.route('/:postID')
.get(blogControllers.getPost);
router.get('/:postID', function (req, res, next) {
blogControllers.getPost;
});
module.exports = router;
Loading…
Cancel
Save