I'm creating a blog using EJS and Node.
After composing,
I get a brief summary of what was written on the homepage, I also get a link which is supposed to send me to the post where I can read the whole composition
Home page code
<%- include('partials/header'); -%>
<body>
<h1>Home</h1>
<p><%= p1 %></p>
<% posts.forEach(function(post) { %>
<h1><%= post.cTitle %></h1>
<p><%= post.cBody.substring(0, 100) "... " %>
<a href="posts/<%=post.link%>">Read more</a></p>
<% }); %>
</body>
</html>
<%- include('partials/footer'); -%>
When I click on the read more link, I get this error:
Heres the app.js code
//jshint esversion:6
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
var _ = require('lodash');;
const homeStartingContent = "Lacus vel facilisis volutpat est velit egestas dui id ornare. Semper auctor neque vitae tempus quam. Sit amet cursus sit amet dictum sit amet justo. Viverra tellus in hac habitasse. Imperdiet proin fermentum leo vel orci porta. Donec ultrices tincidunt arcu non sodales neque sodales ut. Mattis molestie a iaculis at erat pellentesque adipiscing. Magnis dis parturient montes nascetur ridiculus mus mauris vitae ultricies. Adipiscing elit ut aliquam purus sit amet luctus venenatis lectus. Ultrices vitae auctor eu augue ut lectus arcu bibendum at. Odio euismod lacinia at quis risus sed vulputate odio ut. Cursus mattis molestie a iaculis at erat pellentesque adipiscing.";
const aboutContent = "Hac habitasse platea dictumst vestibulum rhoncus est pellentesque. Dictumst vestibulum rhoncus est pellentesque elit ullamcorper. Non diam phasellus vestibulum lorem sed. Platea dictumst quisque sagittis purus sit. Egestas sed sed risus pretium quam vulputate dignissim suspendisse. Mauris in aliquam sem fringilla. Semper risus in hendrerit gravida rutrum quisque non tellus orci. Amet massa vitae tortor condimentum lacinia quis vel eros. Enim ut tellus elementum sagittis vitae. Mauris ultrices eros in cursus turpis massa tincidunt dui.";
const contactContent = "Scelerisque eleifend donec pretium vulputate sapien. Rhoncus urna neque viverra justo nec ultrices. Arcu dui vivamus arcu felis bibendum. Consectetur adipiscing elit duis tristique. Risus viverra adipiscing at in tellus integer feugiat. Sapien nec sagittis aliquam malesuada bibendum arcu vitae. Consequat interdum varius sit amet mattis. Iaculis nunc sed augue lacus. Interdum posuere lorem ipsum dolor sit amet consectetur adipiscing elit. Pulvinar elementum integer enim neque. Ultrices gravida dictum fusce ut placerat orci nulla. Mauris in aliquam sem fringilla ut morbi tincidunt. Tortor posuere ac ut consequat semper viverra nam libero.";
const posts = [];
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
app.get("/", function(req, res){
res.render("home", { p1: homeStartingContent,
posts : posts});
})
app.get("/about", function(req, res){
res.render("about", {p2: aboutContent})
})
app.get("/contact", function(req, res){
res.render("contact", {p3: contactContent})
})
app.get("/compose", function(req, res){
res.render("compose")
})
app.post("/compose", function(req, res){
const post =
{cTitle: req.body.postTitle,
cBody: req.body.postBody,
link: _.lowerCase(req.body.postTitle)}
posts.push(post);
res.redirect("/");
})
app.get('/compose/:newPost', function(req,res){
const resquestedPost = _.lowerCase(req.params.newPost);
posts.forEach(function(post){
const postN = _.lowerCase(post.cTitle);
if (resquestedPost === postN) {
res.render("post",{
title: post.cTitle,
body: post.cBody
});
}
})
})
app.listen(3000, function() {
console.log("Server started on port 3000");
});
CodePudding user response:
You do not have a route to retrieve the post data.
You will need a /posts/:postTitle
endpoint.
Probably you will need to decode the postTitle to go back from 'day 1' to 'day 1'.
app.get('/posts/:postTitle', function(req,res) {
const postTitle = req.params.newPost.toLowerCase();
const post = posts.filter(function(_post) {
return _post.cTitle === decodeURI(postTitle);
});
// after you retrieved to post you need to show you want
// to render the view which would look something like this.
res.render("post", {
title: post.cTitle,
body: post.cBody
});
});
CodePudding user response:
you need to add a /posts
route to your app.js file. Something like this should work:
app.get('/posts/:postId', function(req,res){
const postId = req.params.postId;
let post = // Some query to get more data about the post
res.render("post.ejs", post); // you will need to create a ejs for just displaying data from a single post
});