简体   繁体   English

Express NodeJS中的局部错误

[英]partials error in Express NodeJS

I am trying to use partials in ExpressNodeJS. 我正在尝试在ExpressNodeJS中使用局部函数。

My Node version is 3.x. 我的节点版本是3.x。

My project directory structure is as below. 我的项目目录结构如下。

D:\eclipse\workspace\test

I have created a sub-folder and added headModule.ejs in views as below 我创建了一个子文件夹,并在视图中添加了headModule.ejs ,如下所示

D:\eclipse\workspace\test\views\module\headModule.ejs

Below is my index.ejs file 以下是我的index.ejs文件

index.ejs index.ejs

<!DOCTYPE html>
<html>
<head>

<!-- Include modules -->
<% include module/headModule.ejs %>

<title>Demo</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>

<body>

<!-- Include top part of login page -->
<% include module/top_login.ejs %>

When I run this code, below error is received. 当我运行此代码时,收到以下错误。

Express 500 Error: ENOENT, no such file or directory 'D:\\partials\\headModule.ejs' Express 500错误:ENOENT,没有这样的文件或目录'D:\\ partials \\ headModule.ejs'

Even if I change and adjust path in my index.ejs as 即使我将index.ejs中的路径更改和调整为

<!DOCTYPE html>
<html>
<head>

<!-- Include modules -->
<% include ./views/module/headModule.ejs %>

<title>Demo</title>
<link rel='stylesheet' href='/stylesheets/style.css' />
</head>

<body>

<!-- Include top part of login page -->
<% include ./views/module/top_login.ejs %>

Express 500 Error: ENOENT, no such file or directory 'D:\\eclipse\\workspace\\test\\views\\views\\module\\headModule.ejs' Express 500错误:ENOENT,没有这样的文件或目录'D:\\ eclipse \\ workspace \\ test \\ views \\ views \\ module \\ headModule.ejs'

In short, I tried setting the appropriate paths, but it goes either in root directory as 1st error or finds redundant views folder as 2nd error . 简而言之,我尝试设置适当的路径,但是它要么在根目录中作为第一个错误进入,要么在冗余视图文件夹中作为第二个错误进入。

Any help is appreciated. 任何帮助表示赞赏。 Thanks. 谢谢。

Jay. 周杰伦 Let's imagine that you have the following structure for your views (test is a root directory) : 假设您的视图具有以下结构(test是一个根目录):

test
  views
    partials
    pages

So now if your page is sitting inside pages directory to call partials you need to eg: <% include ../partials/header %> , if you didnt configure your template engine to use different search options for your partials. 因此,现在,如果您的页面位于页面目录中以调用<% include ../partials/header %> ,则您需要例如: <% include ../partials/header %> partials/ <% include ../partials/header %> ,如果您没有将模板引擎配置为对<% include ../partials/header %>使用不同的搜索选项。

Hope this will help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM