大佬教程收集整理的这篇文章主要介绍了无法 POST nodejs 和 mongodb,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
<!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>
@H_772_2@'''
@H_772_2@这是我编写的完整 .Js 代码:
@H_772_2@'''
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",res) {
res.render("admin/dashboard");
});
app.get("/admin/posts",res) {
res.render("admin/posts");
});
app.post("/do-post",res){
blog.collection("posts").insertOne(req.body,document){
res.send("posted succesfully");
});
});
app.get("/posts/:ID",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",res){
blog.collection("posts").update({"_ID": ObjectID(req.body.ID) },{
$push: {
"comments": {author: req.body.author,comment: req.body.comment,email: req.body.email }
}
},post) {
res.send("comment Sucessfull");
});
});
app.Listen(3000,function(){
console.log("Connected");
});
});
@H_772_2@'''
以上是大佬教程为你收集整理的无法 POST nodejs 和 mongodb全部内容,希望文章能够帮你解决无法 POST nodejs 和 mongodb所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。