【问题标题】:local host not responding when ejs templates are added添加 ejs 模板时本地主机没有响应
【发布时间】:2020-06-16 20:12:16
【问题描述】:

将部分(ejs 模板)添加到页面时,本地主机未加载。如果未添加部分,则页面首先加载。但添加部分后,页面不再加载。也没有错误报告。页面不断加载,最后显示无法访问该地址。

var express = require("express");
var app = express();
var bodyparser = require("body-parser");

app.use(bodyparser.urlencoded({extended: true}));
app.set("view engine", "ejs");


app.get("/", function(req , res){
    res.render("landing");
});

app.get("/campgrounds", function(req,res){
   res.render("campgrounds",{campgrounds:campgrounds});
});

app.post("/campgrounds",function(req,res){
    var name = req.body.name;
    var image = req.body.image;
    var newCamp = {name: name, image:image}
    campgrounds.push(newCamp)
    res.redirect("/campgrounds");
});

app.get("/campgrounds/new", function(req, res){
    res.render("new.ejs");
});
app.listen(3000, function(){
    console.log("Yelp camp server has started");
});

当标题添加到以下页面时,页面不再加载

 <%- include('partials/header.ejs') %>

<h1>landing page</h1>

<a href="/campgrounds">view all campgrounds</a>
<p>asdasdasda</p>

<%- include('partials/footer.ejs') %>

请帮忙!!!! 这里也是标题的代码

    <!DOCTYPE html>
<html>
    <head>
        <title>YELPCAMP</title>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" >
    </head>

<body>
<p>header tabs</p>

【问题讨论】:

  • 没用。这仅在添加页眉时发生。如果添加页脚来代替页眉,它就可以工作。但是当添加标题时它不起作用```

标签: node.js express ejs


【解决方案1】:

结束标记中缺少-

<%- include('partials/header.ejs') -%>

...

<%- include('partials/footer.ejs') -%>

【讨论】:

  • 没用。这仅在添加页眉时发生。如果添加页脚来代替页眉,它就可以工作。但是当添加标题时它不起作用
  • @JoelJose 尝试删除 ejs 扩展。看看它是否像这样工作:&lt;%- include('partials/header') -%&gt;
【解决方案2】:

感谢您的帮助, 问题是由于不正确的引导 cdn 和部分声明而发生的。 修复了这些问题,应用现在可以正常运行了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-16
    • 1970-01-01
    • 2015-07-28
    • 1970-01-01
    • 2019-01-16
    • 1970-01-01
    • 2014-10-03
    相关资源
    最近更新 更多