如何使用带有express和EJS的node.js正确“包含”EJS文件

时间:2015-11-22 02:49:10

标签: javascript node.js express ejs

我开始学习node.js,并已安装快递,并使用带快递的EJS。

我有以下代码(主要由快速生成器生成):

app.js

$.ajaxSetup({
   headers: {"X-CSRF-Token": "{{csrfToken}}" }
}); 

index.ejs

var express = require('express');
var path = require('path');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');

var routes = require('./routes/index');

var app = express();

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

app.use('/', routes);

javascripts.ejs

<!DOCTYPE html>
<html>

  <head><% include partials/head.ejs %></head>

  <body>
  <% include partials/header.ejs %>
    <h1><%= title %></h1>
    <p>Welcome to <%= title %></p>

    <% include partials/footer.ejs %>
    <% inlcude partials/javascripts.ejs %>

  </body>
</html>

我的问题是我收到了这个错误:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" integrity="sha512-K1qjQ+NcF2TYO/eI3M6v8EiNYZfA95pQumfvcVrTHtwQVDG+aHRqLi/ETn2uB+1JqwYqVG3LIvdm9lj6imS/pQ==" crossorigin="anonymous"></script>

仅当包含javascripts.ejs时才会发生,如果我删除它,页面加载正常。

这是我的文件夹结构(我遗漏了我认为你不需要的东西,但整个快递结构都在那里)

Unexpected identifier in /var/www/HomeWatch.com/views/index.ejs while compiling ejs

SyntaxError: Unexpected identifier in /var/www/HomeWatch.com/views/index.ejs while compiling ejs
    at Object.Function (<anonymous>)
    at Object.Template.compile (/var/www/HomeWatch.com/node_modules/ejs/lib/ejs.js:464:12)
    at Object.compile (/var/www/HomeWatch.com/node_modules/ejs/lib/ejs.js:288:16)
    at handleCache (/var/www/HomeWatch.com/node_modules/ejs/lib/ejs.js:147:16)
    at View.exports.renderFile [as engine] (/var/www/HomeWatch.com/node_modules/ejs/lib/ejs.js:350:14)
    at View.render (/var/www/HomeWatch.com/node_modules/express/lib/view.js:126:8)
    at tryRender (/var/www/HomeWatch.com/node_modules/express/lib/application.js:639:10)
    at EventEmitter.render (/var/www/HomeWatch.com/node_modules/express/lib/application.js:591:3)
    at ServerResponse.render (/var/www/HomeWatch.com/node_modules/express/lib/response.js:961:7)
    at module.exports (/var/www/HomeWatch.com/routes/index.js:6:7)

1 个答案:

答案 0 :(得分:1)

似乎将javascripts.ejs重命名为javascript.ejs已经解决了问题......我甚至没有勇气问为什么......