Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/40.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 使用express和EJS进行导航_Javascript_Node.js_Express_Ejs - Fatal编程技术网

Javascript 使用express和EJS进行导航

Javascript 使用express和EJS进行导航,javascript,node.js,express,ejs,Javascript,Node.js,Express,Ejs,在了解如何在EJS页面之间有效导航时遇到问题: 文件目录: 我想从index.ejs页面转到about.ejs页面。这是当前未正确导航的my index.ejs页面的代码: index.ejs: 我可以在href中输入什么来正确引用dynamic about.ejs文件? 我已经知道我可以从我的公用文件夹中引用静态文件,但我想引用动态ejs文件夹。如果不可能,任何提供相同功能的解决方案也可以。您应该呈现about.ejs模板,以便在客户端上使用它。为此,您需要创建一条新路线: app.get("

在了解如何在EJS页面之间有效导航时遇到问题:

文件目录: 我想从index.ejs页面转到about.ejs页面。这是当前未正确导航的my index.ejs页面的代码:

index.ejs: 我可以在href中输入什么来正确引用dynamic about.ejs文件? 我已经知道我可以从我的公用文件夹中引用静态文件,但我想引用动态ejs文件夹。如果不可能,任何提供相同功能的解决方案也可以。

您应该呈现about.ejs模板,以便在客户端上使用它。为此,您需要创建一条新路线:

app.get("/about", (req, res) => {
  res.render("about");
});
要打开它,请使用/about path。

您应该呈现about.ejs模板,以便在客户端上使用它。为此,您需要创建一条新路线:

app.get("/about", (req, res) => {
  res.render("about");
});
要打开它,请使用/about路径。

您的链接应指向/about。 那你就得选择了。我在你的服务器上有一个功能来服务该页面。2动态地为页面提供服务

一,

二,

你的链接应该指向/关于。 那你就得选择了。我在你的服务器上有一个功能来服务该页面。2动态地为页面提供服务

一,

二,


您需要为“关于”页面创建路由

app.get("/about", (req, res) => {
    res.render("about")
});
并从超链接中删除扩展名。超链接应为:

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

您需要为“关于”页面创建路由

app.get("/about", (req, res) => {
    res.render("about")
});
并从超链接中删除扩展名。超链接应为:

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

注意第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>

注意第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>
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);