Skip to content

Instantly share code, notes, and snippets.

@tangoabcdelta
Created February 7, 2021 13:47
Show Gist options
  • Save tangoabcdelta/2762c0a4599defc83d5008080081a03d to your computer and use it in GitHub Desktop.
Save tangoabcdelta/2762c0a4599defc83d5008080081a03d to your computer and use it in GitHub Desktop.
keep the file extension of ejs file as .html, 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 an…

keep the file extension of ejs file as .html

  • 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:
1
  • use app.engine('.html', require('ejs').__express); of ejs npm module.

    app.engine('.html', require('ejs').__express); app.set('view engine', 'ejs');

2
  • You can also use :[app.engine(ext, callback)][1]

  • Source: http://expressjs.com/en/4x/api.html#app.engine

  • 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 });

2
  • 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'); });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment