每当我使用EJS语法时页面都拒绝加载,特别是当我使用'include'时

时间:2018-03-30 10:29:11

标签: javascript node.js express ejs

如果我很幸运,页面只加载一次或两次,但拒绝加载大多数时间。但每当我从文件中删除EJS合成文本并只留下HTML它加载正常。我无法弄清楚为什么会这样。

This is the file struture

views
   landing.ejs
   campground.ejs
  /partials
      header.ejs
      footer.ejs

var express = require('express');
var bodyParser = require('body-parser');

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


app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true})); //to use body parser 
app.get('/', function(req, res) {
	res.render('landing');
});
//view campgrounds
app.get('/campgrounds', function(req, res) {
	var campgrounds = [
		{name: '', image: ''}
	]

	res.render('campgrounds', {campgrounds: campgrounds});
});
//post campgrounds
app.post('/campgrounds', function(req, res) {
	//get data from form and add to campground array
	//redirect back to campground page
	res.send('you hit d cmpground post route');
});
//campground form
app.get('/campgrounds/new', function(req, res) {
	res.render('new.ejs');
});



app.listen(port, process.env.PORT, process.env.IP, function() {
	console.log(` Yelp camp has started on port ${port}`)
})
<% include partials/header %>

<h2>landing Page</h2>
<p>welcome to yelp camp</p>

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

 <% include partials/footer %>

app.js

1 个答案:

答案 0 :(得分:0)

我明白了。我需要做的就是用页眉和页脚html标签包装include footer和header语法。

&#13;
&#13;
<header>
	<% include partials/header %>
</header>


<h2>landing Page</h2>
<p>welcome to yelp camp</p>

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

<footer>
	<% include partials/footer %>
</footer>
 
&#13;
&#13;
&#13;