How to: Use ejs without express

前端 未结 3 1361
猫巷女王i
猫巷女王i 2021-02-13 16:09

I\'m starting with node in general, and I\'m attempting to do a site without express. I would none the less want to use ejs to inject my html and this is where my problem is...

3条回答
  •  野性不改
    2021-02-13 16:46

    First of all, You need install ejs -> $ npm install ejs --save

    Simple example:

    main.ejs:

    <%= exampleRenderEjs %>

    server.ejs

    var ejs = require('ejs');
    var fs = require('fs');
    
    var htmlContent = fs.readFileSync(__dirname + '/main.ejs', 'utf8');
    
    var htmlRenderized = ejs.render(htmlContent, {filename: 'main.ejs', exampleRenderEjs: 'Hello World!'});
    
    console.log(htmlRenderized);
    

提交回复
热议问题