You are on page 1of 6

Index.

js

const express = require('express')


const app = express()
const path = require('path')
const methodOverride = require('method-override')
const { v4: uuid } = require('uuid')

app.use(express.urlencoded({ extended: true }))


app.use(express.json())
app.use(methodOverride('_method'))
app.set('view engine', 'ejs')
app.set('views', path.join(__dirname, 'views'))

let comments = [
{
id: uuid(),
username: 'Todd',
comment: 'lol that is so funny'
},
{
id: uuid(),
username: 'skyler',
comment: 'lol that is so great'
},
{
id: uuid(),
username: 'namesh',
comments: 'Nice'
},
{
id: uuid(),
username: 'vishal',
comment: 'woof woof'
},

app.get('/comments', (req, res) => {


res.render('comments/index', { comments })
})

app.get('/comments/new', (req, res) => {


res.render('comments/new')
})
app.post('/comments', (req, res) => {
const { username, comment } = req.body;
comments.push({ username, comment, id: uuid() })
res.redirect('/comments')
})

app.get('/comments/:id', (req, res) => {


const { id } = req.params
const comment = comments.find(c => c.id === id)
res.render('comments/show', { comment })

})

app.get('/comments/:id/edit', (req, res) => {


const { id } = req.params
const comment = comments.find(c => c.id === id)
res.render('comments/edit', { comment })
})

app.patch('/comments/:id', (req, res) => {


const { id } = req.params
const newCommentText = req.body.comment

const foundComment = comments.find(c => c.id === id)


foundComment.comment = newCommentText
res.redirect('/comments')
})

app.delete('/comments/:id', (req, res) => {


const { id } = req.params

comments = comments.filter(c => c.id !== id);


res.redirect('/comments')
})

app.listen(3000, () => {
console.log("I am listening at 3000")
})
views/comments folder

index.ejs

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Comments</title>
</head>

<body>
<h1> Comments</h1>
<ul>
<% for(let c of comments) {%>
<li>
<%= c.comment %> -<b>
<%= c.username %>
</b>
<a href="/comments/<%= c.id %> ">details</a>
</li>

<% } %>
</ul>
<a href="/comments/new">New Comments</a>

</body>

</html>
Show.ejs

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Show</title>
</head>

<body>

<h1>Comments id: <%= comment.id %>


</h1>
<h2>
<%= comment.comment %> - <%= comment.username %>
</h2>
<a href="/comments">Back to Index</a>
<a href="/comments/<%= comment.id %>/edit ">Edit Comment</a>
<form method="POST" action="/comments/<%= comment.id
%>/?_method=DELETE">
<button>Delete</button>
</form>

</body>

</html>
Edit.ejs

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>Edit</title>
</head>

<body>
<h1>Edit</h1>
<form method="POST" action="/comments/<%= comment.id
%>?_method=PATCH">
<textarea name="comment" id="" cols="30" rows="10">
<%= comment.comment %></textarea>
<button>Save</button>
</form>

</body>

</html>
new.ejs

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-
scale=1.0">
<title>New Comments</title>
</head>

<body>
<h1>Make a new comment</h1>
<form action="/comments" method="POST">
<section>
<label for="username">Enter username</label>
<input type="text" id="username" placeholder="username"
name="username">

</section>
<section>
<label for="comment">Comment Text</label>
<br>
<textarea name="comment" id="comment" cols="30"
rows="5"></textarea>
</section>
<button>Submit</button>

</form>
<a href="/comments">Back to index</a>

</body>

</html>

You might also like