简体   繁体   English

使用Express和EJS导航

[英]Navigation using express and EJS

Having a issue figuring out how to navigate between EJS pages effectively: 在解决如何有效地在EJS页面之间导航时遇到问题:

File directory: 文件目录:

在此处输入图片说明

I want to go from my index.ejs page to the about.ejs page. 我想从index.ejs页面转到about.ejs页面。 This is the code for my index.ejs page which is currently not navigating properly: 这是我的index.ejs页面的代码,该页面当前无法正确导航:

index.ejs: index.ejs:

<!DOCTYPE html>
<html>
  <head>
    <title></title>
  </head>
<body>
<h1> <a href="about.ejs"> This is a link to about page</a></h1>
</body>
</html>

app.js server: app.js服务器:

const express = require("express");
const path = require('path');
const app = express();

app.set("views", path.resolve(__dirname, "views"));
app.set("view engine", "ejs")

app.get("/", (req, res) => {
res.render("index")
});

app.use(express.static('public'));

app.listen(3000);

What could I enter in the href to properly reference the dynamic about.ejs file? 我可以在href中输入什么以正确引用动态about.ejs文件? I already know that I can reference static files from my public folder but I want to refernce the dynamic ejs folder. 我已经知道我可以从公共文件夹中引用静态文件,但是我想引用动态ejs文件夹。 If not possible any solution which provides the same functionality will also do. 如果不可能的话,任何提供相同功能的解决方案也可以。

You should render about.ejs template to use it on the client. 您应该渲染 about.ejs模板以在客户端上使用它。 To do that, you need to create a new route: 为此,您需要创建一条新路线:

app.get("/about", (req, res) => {
  res.render("about");
});

To open it use /about path. 要打开它,请使用/about路径。

Your link should point to /about . 您的链接应指向/about Then you have to options. 然后,您必须选择。 1) have a function in your server to serve that page. 1)您的服务器中有一个服务该页面的功能。 2) serve your pages dynamically. 2)动态提供您的页面。

1. 1。

app.get("/about", (req, res) => {
  res.render("about")
});

2. 2。

app.get("/:page", (req, res) => {
  res.render(req.params.page);
});

You need to create a route for your about page 您需要为“关于”页面创建路线

app.get("/about", (req, res) => {
    res.render("about")
});

And remove the extension from the hyperlink. 并从超链接中删除扩展名。 The hyperlink should be : 超链接应为:

<a href="/about">About</a>

Notice the correction in your index.ejs at line 7 请注意第7行的index.ejs中的更正

<!DOCTYPE html>
<html>
  <head>
   <title></title>
  </head>
<body>
  <h1><a href='/about'> About</a></h1>  //there's no point including the ".ejs" extension at the end of the "about"
</body>
</html>

In your app.js server , also notice the addition of the (about route). 在您的app.js服务器中 ,还要注意添加了(关于路由)。

const express = require("express");
const path = require('path');
const app = express();

app.set("views", path.resolve(__dirname, "views"));
app.set("view engine", "ejs")

app.get("/", (req, res) => {
res.render("index")
});

app.get('/about', (req, res)=>{    //here you can include a new "about" route that should take you to the "about" page
    res.render('about')
});

app.use(express.static('public'));

app.listen(3000);

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

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