简体   繁体   English

在 app.post 中为 express JS 更改 URL 字符串

[英]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.

相关问题 使 html 按钮不会触发 node.js 中的 app.post - make html buttons not trigger an app.post in node.js 将EJS变量传递给app.post节点js - Pass EJS variable to app.post node js 我在同一页面上有2个表单,我需要提交按钮才能将信息发送到express.js中正确的app.post,我该怎么做? - I have 2 form on same page and i need the submit button to send the information to the right app.post in express.js, how do i do it? 如何从节点js app.post()调用html内的javascript函数? - How to call a javascript function inside html from node js app.post()? 如何在引导模式(node.js)中的 HTML 视图上显示 app.POST() 结果 - How to display app.POST() result on HTML view in bootstrap modal (node.js) 为什么我的 HTML POST 请求表没有通过 Express app.post? 如何将数字变量传递给另一个 POST 请求? - Why is my HTML POST request form not going through to Express app.post? How do I pass on the number variables to another POST request? App.post 返回空的花括号集 - App.post returning empty set of curly braces app.post 出现问题,导致无法获取 / - Having issues with app.post, results in Cannot Get / 如何在不同页面上将数据从 app.post 传递到 app.get? - How do I pass data from app.post to app.get on different pages? 快速JS POST请求 - Express JS POST Request
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM