最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

可以使用节点获取ejs文件进行渲染

运维笔记admin22浏览0评论

可以使用节点获取ejs文件进行渲染

可以使用节点获取ejs文件进行渲染

我想让我的ejs文件显示在服务器上////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ///////////////////////////

  <html>
          <head><title><%= title %></title></head>
          <body>
            welcome <%= user%>;
          </body>
        </html>









    //////////////////////////////////////////////////////////////////////////////////////


        var express = require("express");
        var app = express();
        var port = process.env.PORT || 3000;

        app.set('view engine', 'ejs');


        app.get("/", function(req, res){
                res.sendFile(__dirname + '/about.html');

        });

        app.get("/news", function(req, res){
                res.sendFile(__dirname + '/news.html');

        });

        //app.get('/student/:id', function(req, rep){
        //        rep.render('student', { name : student[req.params.id] , id : req.params.id});

        //});

        //app.get('/student', function(req, res) {
        //    res.render('student');
        //});

        app.get('/', function(req, res){ 
          res.render('student',{user:"John Smith"}) 
        }); 



        app.listen(port);
回答如下:

您需要创建一个views文件夹,然后将.ejs模板放在其中。例如,如果您创建文件views/student.ejs,则可以使用此路由呈现此ejs文件

app.get('/', function(req, res){ 
    res.render('student', {user:"John Smith"}) 
});

Scotch.io tutorial

发布评论

评论列表(0)

  1. 暂无评论