How to: Use ejs without express

前端 未结 3 1366
猫巷女王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:37

    Example from some book: template.ejs

    
      
        
      
      
        <% entries.map(entry => { %>
          
    <%= entry.title %>
    <%= entry.body %>
    <% }); %> function blogPage(entries) { const values = { entries }; const template = fs.readFileSync('./template.ejs', 'utf8'); return ejs.render(template, values); } const server = http.createServer((req, res) => { const entries = getEntries(); const output = blogPage(entries); res.writeHead(200, {'Content-Type': 'text/html'}); res.end(output); console.log('run server'); }); server.listen(8000);

提交回复
热议问题