【问题标题】:Can get ejs file to render, im using node可以让ejs文件渲染,我使用节点
【发布时间】:2019-09-25 16:49:57
【问题描述】:

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

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

【问题讨论】:

  • 你的问题不清楚,你面临的问题是什么?
  • 我想让student.ejs文件显示在服务器上
  • 您有两个具有相同路由的映射,您想为学生使用/student 吗?
  • 是的,//////////////////////

标签: javascript node.js ejs


【解决方案1】:

您为'/' 配置了冲突的路由。

您想为学生使用单独的路线吗?喜欢关注

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

【讨论】:

    【解决方案2】:

    您需要创建一个views 文件夹,并将您的 .ejs 模板放入其中。例如,如果您创建文件 views/student.ejs ,那么您可以使用此路由来渲染此 ejs 文件

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

    Scotch.io tutorial

    【讨论】:

      猜你喜欢
      • 2018-10-26
      • 1970-01-01
      • 2016-09-14
      • 2018-04-14
      • 1970-01-01
      • 2018-09-30
      • 2023-03-31
      • 1970-01-01
      • 2019-09-16
      相关资源
      最近更新 更多