2

I'm new to express framework i just want know how to render html page with some data from mongo DB i used below code to send html but not sure how to send some data

res.sendFile(path + "feature.html"); this is success as html get renders 

send data like this ??

res.sendFile(path + "feature.html", {data: data});

how to display data in html ?? if i have sent an array like this

res.sendFile(path + "feature.html", {data: []});

how to loop this in html ??

2 Answers 2

7

I suggest you to use some template engine, like ejs or jade. This form, you can use res.render and send json information for view. For more details consult the engine docs and res.render doc too.

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

Comments

2

If you need to render an html page on node.js express, you need to install any of the template engine like ejs or jade and set the view directory. The code used to install ejs using npm is,

npm install ejs --save

include the following code into your app.js file will resolve your problem.

var bodyParser = require('body-parser');
var express = require('express');
var app = express();

app.use(express.static(__dirname + '/'));
app.use(bodyParser.urlencoded({extend:true}));
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'ejs');
app.set('views', __dirname);

app.get('/', function(req, res){
    res.render("index");
});

2 Comments

After edit this line app.set('view engine', 'html'); to app.set('view engine', 'ejs'); running fine.
It's better if you added that "how data passed via render".

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.