- While developing an expressjs app using ejs templating engine you may want to use keep the extension as
*.html
instead of*.ejs
- e.g.
home.html
instead of using home.ejs. - Main reason for using html is generally to use more effective code hint, formatting and syntax highlighting without adding any extra settings. To accomplish it, you can perform any of the following:
-
use
app.engine('.html', require('ejs').__express);
of ejs npm module.app.engine('.html', require('ejs').__express); app.set('view engine', 'ejs');
-
You can also use :
[app.engine(ext, callback)][1]
-
Set the following up with your view engine and views settings near the top:
app.engine('html', require('ejs').renderFile);
-
Call
res.render
-
The only difference is that you need to specify the extension:
-
In the following example, you'll see that the variables are handled as you would expect it to in an
.ejs
file.app.get('/',function(req,res){ res.render("home.html", { myString: "I'm a normal string!" });
//<%=myString%>
is handled like it'd be in a normal.ejs
file });
-
Alternatively,
const app = require('express')(); const engine = require('ejs'); app.engine('html', engine.__express); app.set('views', path.join(__dirname, './views')); app.set('view engine', 'html');
app.engine('.html', require('ejs').__express); app.set('view engine', 'ejs');
//this route will open profil.html in folder ./views app.get('/', (req, res) => { //render file profil.html res.render('profil', { name: 'Danang Ponorogo' }); });
var server = http.createServer(app); server.listen(3001, () => { console.log('Server is running at port 3001'); });