From 3460f9ad874495469cfa8d4aaa42b66f5e427bf2 Mon Sep 17 00:00:00 2001 From: Mateja Date: Mon, 3 May 2021 00:56:35 +0200 Subject: Use MongoDB (with Mongoose) to store posts. --- routes/web.js | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'routes') diff --git a/routes/web.js b/routes/web.js index a1a7b01..49b9b85 100644 --- a/routes/web.js +++ b/routes/web.js @@ -1,10 +1,14 @@ const express = require('express'); const router = express.Router(); +const Post = require('../models/post'); router.get('/', (req, res) => { - res.render('home', { - title: 'Home Page', - home: true + Post.find().lean().exec((err, posts) => { + res.render('home', { + title: 'Home Page', + home: true, + posts: posts + }); }); }); @@ -16,8 +20,11 @@ router.get('/new-post', (req, res) => { }); router.post('/new-post', (req, res) => { - console.log(req.body); - res.redirect('/'); + const newPost = new Post({ + 'title': req.body.title, + 'text': req.body.text + }); + newPost.save().then(() => res.redirect('/')); }); module.exports = router; -- cgit v1.2.3