Node.js 对不同的html页面使用href标记,但在express中使用相同的url id
我试图通过点击每个按钮在同一用户的主页和个人资料页面之间切换。除主页和个人资料之间的更改外,id应保持不变 但在点击profile按钮后,只有id会被更改,它使用profile作为id (我使用ejs作为视图/html页面的格式) 知道我该怎么修吗?可能吗 这是我的导航代码:Node.js 对不同的html页面使用href标记,但在express中使用相同的url id,node.js,express,url,parameters,ejs,Node.js,Express,Url,Parameters,Ejs,我试图通过点击每个按钮在同一用户的主页和个人资料页面之间切换。除主页和个人资料之间的更改外,id应保持不变 但在点击profile按钮后,只有id会被更改,它使用profile作为id (我使用ejs作为视图/html页面的格式) 知道我该怎么修吗?可能吗 这是我的导航代码: <nav> <div class="nav-wrapper teal darken-4"> <a href="#!" class=
<nav>
<div class="nav-wrapper teal darken-4">
<a href="#!" class="brand-logo">BAZAART</a>
<ul class="right hide-on-med-and-down">
<li><a class="waves-effect waves-light btn teal lighten-1" href="home"> <i class="material-icons right">home</i> home</a></li>
<li><a class="waves-effect waves-light btn teal lighten-1" href="profile">profile <i class="material-icons right">account_box</i></a></li>
</ul>
</div>
</nav>
main.js
app.get("/profile/:myName", homeController.respondWithName);
app.get("/", homeController.respondInfo);
app.get("/home/:userHome", homeController.sendReqParam)
我是这样想的:
我最近在制作一个博客网站,在那里我写了一篇文章,并将其显示在主页上。但是,如果我们想进入特定的帖子页面,而不是为每个新帖子创建另一个单独的页面,我们会创建一个post.ejs
页面,然后访问特定的帖子,我们只需要使用一种称为lodash的东西。我将向您展示它的一个示例,因此它更有意义,我将向您展示我们使用的代码
例如,我进入compose.ejs
页面,写了一篇随机文章:title=post,content=andom lorem ipsum
假设我们写了另一篇文章:title=另一篇文章,content=另一篇随机的lorem ipsum
好的,现在每次我们写一篇博客文章,它都会将我们发送到主页(我们现在所在的位置),并显示这两篇博客文章。如果我们想转到文章的特定url,我们只需编写此链接localhost:3000/posts/other post
点击回车键,就可以转到我们编写的第二篇文章
这是我们在app.js
中使用的代码:
app.get("/posts/:postName", function(req, res){
const requestedTitle = _.lowerCase(req.params.postName);
posts.forEach(function(post) {
const storedTitle = _.lowerCase(post.title);
if (storedTitle === requestedTitle) {
res.render("post", {title: post.title, content: post.content});
}
});
});
<%- include("partials/header") -%>
<br>
<div class="card">
<h2 class="card-header"> <%= title %> </h2>
<div class="card-body">
<p class="card-text"> <%= content %> </p>
</div>
</div>
<%- include("partials/footer") -%>
在app.js
代码中,我们在app.get/posts/:postName
中看到,这正是将要显示在url中的名称,:postName
就像一个变量,它将存储用户写入的任何内容
在第二行中,我们使用lodash将用户编写的内容重写为我们想要的内容,例如,如果用户编写了另一篇文章
,它将自动将其更改为另一篇文章
,我们将其存储在名为requestedTitle
的常量中
接下来是posts
数组上的forEach循环(我们存储每个post),这只是遍历每个post并检查名称
在第4行中,我们再次使用lodash来表示同样的内容,但这一次是围绕每个帖子的标题,并将其存储在名为storedTitle
的常量中
最后是一个if语句,如果两个名称相同,那么它将呈现post.ejs
页面,我们只需使用此代码,{title:post.title,content:post.content}
传递所选文章的标题和内容
这是我们在post.ejs
中使用的代码:
app.get("/posts/:postName", function(req, res){
const requestedTitle = _.lowerCase(req.params.postName);
posts.forEach(function(post) {
const storedTitle = _.lowerCase(post.title);
if (storedTitle === requestedTitle) {
res.render("post", {title: post.title, content: post.content});
}
});
});
<%- include("partials/header") -%>
<br>
<div class="card">
<h2 class="card-header"> <%= title %> </h2>
<div class="card-body">
<p class="card-text"> <%= content %> </p>
</div>
</div>
<%- include("partials/footer") -%>
很明显,有一个表单供你写文章,在你提交后,它会将你发送到主页,并保存文章。为此,我使用了以下代码:
app.post("/compose", function(req, res){
const post = {
title: req.body.postTitle,
content: req.body.postBody
};
posts.push(post);
res.redirect("/");
});
我对你的网站有一个想法,如果当你按下Profile按钮时,它会在你的网站上呈现一个特定的页面,当你按下另一个按钮时,它会呈现另一个页面。它可以工作,不是吗
请尝试一下并告诉我它是如何运行的。然后它在没有Id的情况下响应如下:localhost:8080/profile。但是应该是这样的:localhost:8080/profile/lilyThen您应该通过jsp在href中追加
lily
,谢谢您的帮助!我会在这个周末尝试解决方案,并让你知道!