How do I render an EJS template file in Node.js?

前端 未结 8 1707
臣服心动
臣服心动 2020-12-05 05:24

I\'m using Node.js and trying to render an EJS template file. I figured out how to render strings:

    var http = requ         


        
8条回答
  •  [愿得一人]
    2020-12-05 05:55

    Simply set express view engine to "ejs"

    const express = require("express");
    const app = express();
    
    // template engine config
    app.set("view engine", "ejs");
    app.set("views", "views"); // setting the folder that has our views & it is default value even if not declared
    
    app.get("/", (req, res, next) => {
      res.render("index");
    });
    

    and of course you need to set your directory as so:

    • views (contains all main & partial views).
    • app.js (has the above configurations).

    And that is it.

提交回复
热议问题