试图通过javascript和html

时间:2016-03-13 02:08:59

标签: javascript html node.js mongodb

我对mongodb,节点等相当新,我试图执行一个简单的任务,将博客帖子插入我已经创建的数据库中,我决定做的是制作一个网页按下一个按钮,然后插入我的mongo数据库,但不幸的是我没有这样做。我知道要插入我必须使用终端,例如节点server.js然后它会插入,但是我希望只需按下html页面上的按钮即可。因此,任何有关如何做的建议将不胜感激,以下是我目前的代码。

<!DOCTYPE html>
<html>
<head>
Add Blog Post
</head>
<body>
<h1>
Welcome to your Blog! Feel free to add a blog post!
</h1>
<form>
Title: <br>
<input type = "text" name = "Title" id = "title"><br>
Blog Post: <br>
<input type = "text" name ="EnterBlog" id = "blog"> <br>

<input type = "button" value = "Submit Blog" onclick="
var insertDocuments = function(db, callback) {
  // Get the documents collection
 var collection = db.collection('blog');
 var title = Title.value;
 var blog = EnterBlog.value;
  // Insert some documents
  collection.insertMany([
    {Title: title}, {Blog : blog}
  ], function(err, result) {
    assert.equal(err, null);
    assert.equal(2, result.result.n);
    assert.equal(2, result.ops.length);
    console.log('Inserted 2 documents into the Blog collection');
    callback(result);
  });
}
exports.insertDocuments = insertDocuments;

var MongoClient = require('mongodb').MongoClient
  , assert = require('assert');

// Connection URL
var url = 'mongodb://localhost:27017/blogapp';
// Use connect method to connect to the Server
MongoClient.connect(url, function(err, db) {
  assert.equal(null, err);
  console.log('Connected correctly to server');

  insertDocuments(db, function() {
    db.close();
  });
});"
>
</form>

</body>
</html>

0 个答案:

没有答案