0

How to insert html tags within mongodb using nodejs

I want to insert html files into mongodb using nodejs , how will be the code structure ?

3
  • 1
    possible dup stackoverflow.com/questions/15275599/… Commented Oct 20, 2016 at 6:26
  • Maybe you should try and have a look at the documentation first before asking a question without trying at all. docs.mongodb.com/getting-started/node/insert Commented Oct 20, 2016 at 6:26
  • What do you mean by code structure? Could you elaborate with examples? Commented Oct 20, 2016 at 7:51

2 Answers 2

1

use this one, example of get html code frm url and save into databse

 var mongoose = require("mongoose");
 var request = require('request');

 var dbURI = 'mongodb://localhost:27017/SOF'; 

 // Create the database connection 
 mongoose.connect(dbURI); 

 // CONNECTION EVENTS
 // When successfully connected
 mongoose.connection.on('connected', function () {  
 console.log('Mongoose default connection open to ' + dbURI);
 }); 

 // mongodb schema
 var dataschema = new mongoose.Schema({
  ofString:   String,
 })


 var model = mongoose.model("Data", dataschema); 
 var Data = mongoose.model("Data");
 var data = new  Data();

   /*
     i have used request node module to get html source code from url       and   insert into database
   */ 
 request('http://www.google.com', function (error, response, body) {
    if (!error && response.statusCode == 200) {     
       data.ofString = body;

      data.save(function(err, result) {
         if(err)
           console.log(err);
             else
            console.log(result);
       }); 
         }
       })

     // find data
     model.find({}, function(err, result) {
        if(err)
          console.log(err);
          else
         console.log(result);

         });
Sign up to request clarification or add additional context in comments.

Comments

0

You can store HTML parts and files in MonDOB as standard UTF-8 encoded strings, look through the documentation: MongoDB Fundamentals, and Insert Data with Node

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.