javascript - 如何使用带有 express 和 EJS 的 node.js 正确地 "include"EJS 文件

标签 javascript node.js express ejs

<分区>

我开始学习 node.js,并且已经安装了 express 并且正在使用 EJS 和 express。

我有以下代码(主要由 express-generator 生成):

app.js

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

index.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>

javascripts.ejs

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

我的问题是出现了这个错误:

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)

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

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

-bin
--www
routes
-index.js
views
-partials
--footer.ejs
--head.ejs
--header.ejs
--javascripts.ejs
-error.ejs
-index.ejs
app.js
package.json

最佳答案

看来将javascripts.ejs重命名为javascript.ejs就解决了问题……我连问为什么的勇气都没有……

关于javascript - 如何使用带有 express 和 EJS 的 node.js 正确地 "include"EJS 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33851101/

相关文章:

javascript - HTML5 Canvas 上三 Angular 形每个顶点的渐变

Node.js 多中间件导出模式

javascript - 在chrome扩展中获取 "Remote Address"(IP地址)

node.js - NodeJS -> Mongodb 查询

node.js - 用node实现redis时,如何将多个json对象添加到hmset或hset或任何数据类型?

javascript - 在 Java 项目中使用 JavaScript 字符串文字

javascript - 如何在 mongodb v3.6 中排序

Javascript:在 Express 应用程序中分解出相同的回调

javascript - Laravel 使用 webpack mix 导入 javascript 自定义文件

php - 图片上传、调整MySQL存储路径大小