[英]Changing the URL string in app.post for express JS
this is my first post and I am trying to get back to technology after a break of few years.这是我的第一篇文章,我正试图在休息几年后回到技术上。 Really enjoying coding again and decided to learn the MERN stack first.
再次真正享受编码并决定先学习 MERN 堆栈。 I went through some tutorials and then started to build a website for my business.
我浏览了一些教程,然后开始为我的企业建立一个网站。 Created a few pages here and there and was fairly successful to build most things via express, HTML and CSS, until I hit a situation.
在这里和那里创建了几个页面,并且通过 express、HTML 和 CSS 构建大多数东西相当成功,直到我遇到问题。
Question.- I have an app.post for my '/ranking' route and in this function I am performing some logic.问题。-我的“/排名”路线有一个 app.post,在这个 function 中,我正在执行一些逻辑。 This app.post is being called from the '/ranking' page when the user hits the "View Profile" button.
当用户点击“查看个人资料”按钮时,正在从“/排名”页面调用此 app.post。 The idea is to pick up unique ID as the value from the HTML template, pass it onto app.post.
想法是从 HTML 模板中获取唯一 ID 作为值,将其传递到 app.post。 Then perform the business logic, which includes finding the unique values of the unique ID from the DB.
然后执行业务逻辑,包括从数据库中查找唯一 ID 的唯一值。 Once this is done, pass it over to a /profile page with res.render.
完成后,将其传递到带有 res.render 的 /profile 页面。 Now this is all working fine.
现在这一切正常。 However, after the res.render("profile") call, even though the website is going correctly to the page that I want it to, the url reads http://localhost:3000/ranking (This is the page from where the post call made).
但是,在 res.render("profile") 调用之后,即使网站正确地转到我想要的页面,url 也会读取http://localhost:3000/ranking (这是打电话后)。 Instead I want it to read, http://localhost:3000/profile
相反,我希望它阅读http://localhost:3000/profile
If there is a solution for this please do let me know.如果有解决方案,请告诉我。 Also if the answer implies that I haven't understood certain key concepts about express and routing, please pass me in that direction.
此外,如果答案暗示我不了解有关快递和路由的某些关键概念,请按这个方向传给我。 Thanks in advance.
提前致谢。
The URL of the route and the filename you pass to render
are entirely independent.路线的 URL 和您传递给
render
的文件名是完全独立的。
If you want to redirect someone to a different URL, then you need to respond to the request using res.redirect
and not res.render
.如果您想将某人重定向到不同的 URL,那么您需要使用
res.redirect
而不是res.render
来响应请求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.