无法在 node.js 网络应用程序中显示 EJS 页面
Cannot display EJS page in node.js web app
我有一个简单的 nodejs 应用程序,具有这样的文件夹结构(不是完整的结构,但足以解决这个问题)。
index.js
views
- index.ejs
- about.ejs
在index.js
,我有
app.use("/", (request, response) => {
response.render('index')
})
app.use("/about", (request, response) => {
response.render('about')
})
// Also tried with extension (index.ejs and about.ejs)
在index.ejs
,我有
<a href = "../about"> About </a>
问题
- 当我进入 root url (
localhost:port/
) 时,显示 index.ejs。
- 在index.ejs中,我点击了link
About
,url变成了(localhost:port/about
),但是about.ejs
未呈现。但是,在浏览器(Microsoft Edge Chrominum)中检查 F12 源,about.ejs
就在那里。
- 当我通过输入 url 到 url 栏直接转到 link 时,显示
index.js
。与步骤 2 相比,行为(在 F12/sources 中)相同。
- 当我注释掉(
index.js
中的 app.use("/")
块时,about.ejs 可以访问并显示在屏幕上。
- 注意,如果它们是 html 页面并由 node.js 服务器提供服务,则导航工作完美。一切都只在本地服务器上测试过。
问题
这是什么原因以及如何解决?
为什么要使用 EJS?
我想创建一个基于数据库查询的动态前端。
在 Whosebug 上似乎有多个关于此的问题,我已经尝试了多个问题并且 none 解决了我的问题,或者清楚地解释了原因。
正如@Quentin 回答的那样,问题是由于我使用了 use
。对于在本例中为 GET
的请求,我必须使用 get
函数,它解决了问题。
use
适用于我想分解我的index.js或使用中间件。
我有一个简单的 nodejs 应用程序,具有这样的文件夹结构(不是完整的结构,但足以解决这个问题)。
index.js
views
- index.ejs
- about.ejs
在index.js
,我有
app.use("/", (request, response) => {
response.render('index')
})
app.use("/about", (request, response) => {
response.render('about')
})
// Also tried with extension (index.ejs and about.ejs)
在index.ejs
,我有
<a href = "../about"> About </a>
问题
- 当我进入 root url (
localhost:port/
) 时,显示 index.ejs。 - 在index.ejs中,我点击了link
About
,url变成了(localhost:port/about
),但是about.ejs
未呈现。但是,在浏览器(Microsoft Edge Chrominum)中检查 F12 源,about.ejs
就在那里。 - 当我通过输入 url 到 url 栏直接转到 link 时,显示
index.js
。与步骤 2 相比,行为(在 F12/sources 中)相同。 - 当我注释掉(
index.js
中的app.use("/")
块时,about.ejs 可以访问并显示在屏幕上。 - 注意,如果它们是 html 页面并由 node.js 服务器提供服务,则导航工作完美。一切都只在本地服务器上测试过。
问题 这是什么原因以及如何解决?
为什么要使用 EJS? 我想创建一个基于数据库查询的动态前端。
在 Whosebug 上似乎有多个关于此的问题,我已经尝试了多个问题并且 none 解决了我的问题,或者清楚地解释了原因。
正如@Quentin 回答的那样,问题是由于我使用了 use
。对于在本例中为 GET
的请求,我必须使用 get
函数,它解决了问题。
use
适用于我想分解我的index.js或使用中间件。