Hi I am trying to create HTML into my .ejs file. I saved the HTML line into MongoDB "html":"<p>HelloWorld</p>"
MongoDB
{ "_id" : ObjectId("53fbc146e93aea8f0bc7c904"), "date" : ISODate("2014-08-25T23:05:42.459Z"), "firstname" : "ali", **"html" : "<p>Hello World</p>"**, "post1" : { "text" : "hello", "date" : ISODate("2014-08-26T15:09:29.338Z") } }
My Node.js application looks like this:
Node.js App
var express = require('express');
var port = 3000;
var ejs = require('ejs');
var MongoClient = require('mongodb').MongoClient,
ObjectID = require('mongodb').ObjectID,
url = require('url');
var db;
//var mongo;
var app = express();
app.use(bodyParser());
app.use(express.static(__dirname + '/public')); //setup static public directory
app.set('views', __dirname + '/views'); //optional since express defaults to CWD/views
app.set('view engine', 'ejs');
// Start server
app.listen(port);
console.log('App started on port ' + port);
// Initialize connection once
MongoClient.connect("mongodb://localhost:27017/testDB", function(err, database) { //"mongodb://localhost:27017/test"
if(err) throw err;
db = database;
});
app.get('/user',function(req, res) {
db.collection('user', function(err, collection){
collection.findOne({'_id':ObjectID("53fbc146e93aea8f0bc7c904")},function(err, user) {
console.log(user.html);
res.render('user.ejs', {test:user.html});
});
});
});
In the following I am displaying the saved html but the problem is it appears not "HellowWorld" it appears <p>HelloWorld</p>
with the tags. How can I display it without the tags?
User.ejs
<!DOCTYPE html>
<html>
<head>
</head>
<body>
**<p><%=test%></p>**
</body>
</html>