aboutsummaryrefslogtreecommitdiff
path: root/routes
diff options
context:
space:
mode:
authorMateja <mail@matejamaric.com>2021-05-03 00:56:35 +0200
committerMateja <mail@matejamaric.com>2021-05-03 00:56:35 +0200
commit3460f9ad874495469cfa8d4aaa42b66f5e427bf2 (patch)
tree5595313907d53ee8bcb5f3e8ca684b7a44126ee2 /routes
parentc2649bfff51a175508685e493d8bfae4713cda16 (diff)
downloadnode-playground-3460f9ad874495469cfa8d4aaa42b66f5e427bf2.tar.gz
node-playground-3460f9ad874495469cfa8d4aaa42b66f5e427bf2.zip
Use MongoDB (with Mongoose) to store posts.
Diffstat (limited to 'routes')
-rw-r--r--routes/web.js17
1 files changed, 12 insertions, 5 deletions
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;