'Cannot POST nodejs and mongodb
Iam trying to add comment to the blogs on the blog page but i cannot find the error ,I keep getting the error "CANNOT POST /" after clicking the submit button on the form. What is missing and what's the problem? I tried the code below for my form:
here is my complete .ejs code for that page . '''
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/static/user/css/style.css">
</head>
<body>
<div class="row">
<div class="leftcolumn">
<div class="card">
<h2><%=post.title%></h2>
<h5>Title description, Dec 7, 2017</h5>
<div class="fakeimg" style="height:200px;">Image</div>
<p><%=post.content%></p>
</div>
<div id="respond" class="comment-respond">
<h3 id="reply-title" class="comment-reply-title">
Leave a Reply <small><a rel="nofollow" id="cancel-comment-reply-link" href="/warning-over-dangerous-diy-beauty-trends-on-tiktok/#respond" style="display:none;">Cancel reply</a></small>
</h3>
<form method="POST" onsubmit="return doComment(this);">
<input type="" name="post_id" value="<%=post._id%>" />
<div class='form-group'>
<textarea placeholder="Comment" name="comment" cols="45" rows="8" required></textarea>
</div>
<div class='form-group'>
<input placeholder="Name (required)" name="author" type="text" required />
</div>
<div class='form-group'>
<input placeholder="Email (required)" name="email" type="email" required />
</div>
<input type="submit" class="waves-effect waves-light btn-large" value="Post Comment">
</form>
<script>
function doComment(form){
$.ajax({
url:"/do-comment",
method:"POST",
data:{author: form.author.value,
comment: form.comment.value,
post_id: form.post_id.value,
email: form.email.value},
success: function (response){
alert(response);
}
});
return false;
}
</script>
</div>
</div>
<div class="rightcolumn">
<div class="card">
<h2>About Me</h2>
<div class="fakeimg" style="height:100px;">Image</div>
<p>Some text about me in culpa qui officia deserunt mollit anim..</p>
</div>
<div class="card">
<h3>Popular Post</h3>
<div class="fakeimg"><a href="<%=post._id%>">Image</a></div><br>
</div>
<div class="card">
<h3>Follow Me</h3>
<p>Some text..</p>
</div>
</div>
</div>
<%- include('footer'); -%>
</body>
</html>
'''
here is my complete .js code i have written :
'''
var express =require("express");
var app=express();
var bodyParser=require("body-parser");
var ObjectId = require("mongodb").ObjectId;
app.use("/static",express.static(__dirname + "/static"));
app.set("view engine","ejs");
app.use(bodyParser.urlencoded({
extended : false
}));
app.use(bodyParser.json());
var MongoClient =require("mongodb").MongoClient;
MongoClient.connect("mongodb://localhost:27017",{useUnifiedTopology: true},
function(error, client){
var blog=client.db("blog");
console.log("DB Connected");
app.get('/', function(req,res) {
blog.collection("posts").find().sort({"_id":-1}).toArray(function(error,posts){
res.render("user/home",{posts: posts}) ;
});
});
app.get("/admin/dashboard", function(req,res) {
res.render("admin/dashboard");
});
app.get("/admin/posts", function(req,res) {
res.render("admin/posts");
});
app.post("/do-post",function(req,res){
blog.collection("posts").insertOne(req.body, function(error, document){
res.send("posted succesfully");
});
});
app.get("/posts/:id", function(req, res){
console.log("Comment");
blog.collection("posts").findOne({"_id": ObjectId(req.params.id)}, function (error, post) {
res.render("user/post", {post: post});
});
});
app.post("/do-comment", function(req, res){
blog.collection("posts").update({"_id": ObjectId(req.body.id) }, {
$push: {
"comments": {author: req.body.author, comment: req.body.comment, email: req.body.email }
}
}, function (error, post) {
res.send("comment Sucessfull");
});
});
app.listen(3000, function(){
console.log("Connected");
});
});
'''
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
