使用Express JS路由将POST重定向到POST [英] Redirect POST to POST using Express JS routes

查看:187
本文介绍了使用Express JS路由将POST重定向到POST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个着陆页('/')。在此页面上是一个用户名和密码的登录表单,提交按钮。我有app.get加载页面和一个app.post来接收表单的参数。但是,我的目的是要有一个app.post('/ login',function(req,res){// logic});没有/登录路由或页面加载,这将简单地容纳登录本身的逻辑(验证用户名等)是否有一个简单的方法来从'/'重定向到post /登录没有它试图加载/登录页面,这不存在?

解决方案

我不知道如果我明白你的问题正确据了解,您希望将POST请求 / 重定向到POST请求到 / login ,这可能很棘手。 / p>

通常,您通过发送301移动永久或302找到HTTP状态代码来进行重定向。他们通常在实践中工作,就好像是303请参阅其他和GET请求。维基百科上的 HTTP状态代码列表


这是与标准相矛盾的行业实践的一个例子。
HTTP / 1.0规范(RFC 1945)要求客户端执行一个
临时重定向(原始描述短语Moved
Temporarily),但流行的浏览器实现了302与
的功能303见其他。因此,HTTP / 1.1添加状态
代码303和307以区分两种行为。
但是,一些Web应用程序和框架使用302状态代码
,就像它是303.


有一个307临时重定向(自HTTP / 1.1)创建,以解决这个问题,不允许更改HTTP方法 - 所以POST的重定向仍然是POST - 请参阅维基百科:


在这种情况下,请求应该与另一个URI重复;
然而,未来的请求应该仍然使用原始的URI。在
中,与历史上执行的302相反,在重新发出原始请求时,不允许更改请求方法
。对于
示例,应该使用另一个POST
请求重复POST请求。


据我所知浏览器应该在遵循这样的重定向之前警告用户。我不知道在实践中是如何工作的。我不会依赖这一点,因为它可能令用户感到烦人,甚至根本不工作。



我的建议是改变这一点:

  app.post('/ login',function(req,res){
// logic
}) ;

到:

  function loginHandler(req,res){
// logic
}
app.post('/ login',loginHandler);
app.post('/',loginHandler);

或使用正则表达式:

($ / $ / $ / $) ;

而不是重定向,这两个路由将使用相同的处理程序。它会更快,更强大,因为你不要依赖于你所需的重定向工作 - 根本没有重定向。


So I have a landing page ('/'). On this page is a login form for a username and password with a submit button. I have the app.get that loads the page and an app.post to receive the parameters of the form. However, my intention is to have an app.post('/login', function (req, res){ //logic}); There is no /login route or page to load, this would simply house the logic for the login itself (validation of the username, etc.) Is there an easy way to get the post from '/' to redirect into the post for /login without it trying to load the /login page, which does not exist?

解决方案

I'm not sure if I understand your question correctly. As I understand you want your POST request to / get redirected to POST requests to /login which can be tricky.

Usually you do redirects by sending either 301 Moved Permanently or 302 Found HTTP status code. Both of them usually work in practice as if it was 303 See Other and a GET request is made. See List of HTTP status codes on Wikipedia:

This is an example of industry practice contradicting the standard. The HTTP/1.0 specification (RFC 1945) required the client to perform a temporary redirect (the original describing phrase was "Moved Temporarily"), but popular browsers implemented 302 with the functionality of a 303 See Other. Therefore, HTTP/1.1 added status codes 303 and 307 to distinguish between the two behaviours. However, some Web applications and frameworks use the 302 status code as if it were the 303.

There is a 307 Temporary Redirect (since HTTP/1.1) created to address this issue that is not allowed to change the HTTP method - so a redirect from POST should still be POST - see Wikipedia:

In this case, the request should be repeated with another URI; however, future requests should still use the original URI. In contrast to how 302 was historically implemented, the request method is not allowed to be changed when reissuing the original request. For example, a POST request should be repeated using another POST request.

As far as I know browsers should warn users before following such redirects. I don't know how that works in practice. I wouldn't rely on that since it can be annoying to users at best or even not work at all.

My suggestion would be to either change this:

app.post('/login', function (req, res) {
  // logic
}); 

to this:

function loginHandler(req, res) {
  // logic
}
app.post('/login', loginHandler); 
app.post('/', loginHandler); 

or something like this, using regular expressions:

app.post(/^\/(?:login)?$/, function (req, res) {
  // logic
}); 

That way instead of a redirect the same handler will be used for both of those routes. It would be both faster and more robust because that way you don't rely on the redirect working as you want - there is no redirect at all.

这篇关于使用Express JS路由将POST重定向到POST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆