EJS导入(使用Express)不会呈现局部图片

时间:2019-07-09 19:22:10

标签: node.js express ejs

我正在尝试将局部视图渲染成视图,但是我不断收到错误消息

Error: Could not find the include file "('partials/header')"

我的文件结构如下:

-MAIN FOLDER
 -controllers
   -userController
   -staticPagesController
   -postsController
 -models
    posts
    users
 -public
    stylesheets
 -views
    user
      all user ejs files
    post
      all post ejs files
    site
      all staticpage ejs files
    partials
      all partials
 -index.js

我尝试将我的partials文件夹添加到每个视图子文件夹,但没有任何帮助。 我还认为也许我的控制器未正确看到该路径,并向每个控制器路径添加了ejs = require({ejs),但它也没有解决。

我的观点如下:

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

<h1>Homepage</h1>

我的index.js看起来像这样:

//Application global constants
const express    = require(`express`);
const mongoose   = require(`mongoose`);
const bodyParser = require(`body-parser`);
const ejs        = require('ejs');
const _          = require(`lodash`);      
//Initiate Express
const app = express();

app.use(express.static(`public`));
//Initiate EJS engine
app.set(`view engine`, `ejs`);
app.engine('html', require('ejs').renderFile);
//Initiate body parser
app.use(bodyParser.urlencoded({extended: true}));
//Set express static files


//ROUTING
staticPages = require(`./controllers/staticPageController`);
postPages = require(`./controllers/postController`);
userPages = require(`./controllers/userController`);

2 个答案:

答案 0 :(得分:0)

请您尝试添加以下代码:

var express = require('express');
var app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');

还包含标题(例如,删除空格):

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

答案 1 :(得分:0)

添加“视图”文件夹声明

app.set('views',path.join(__ dirname,'views'))); app.set('view engine','ejs');