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

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

问题描述

所以我有一个登陆页面('/').在此页面上是一个带有提交按钮的用户名和密码登录表单.我有加载页面的 app.get 和一个 app.post 来接收表单的参数.然而,我的意图是有一个 app.post('/login', function (req, res){//logic});没有要加载的/login 路由或页面,这只会包含登录本身的逻辑(验证用户名等)是否有一种简单的方法可以从/"获取帖子以重定向到/登录而不尝试加载/login 页面,该页面不存在?

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?

推荐答案

我不确定我是否正确理解了您的问题.据我了解,您希望将 / 的 POST 请求重定向到 /login 的 POST 请求,这可能很棘手.

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.

通常您通过发送 301 Moved Permanently 或 302 Found HTTP 状态代码来进行重定向.它们通常在实践中工作,就好像它是 303 See Other 并发出 GET 请求.请参阅维基百科上的HTTP 状态代码列表:

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:

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

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.

创建了一个 307 临时重定向(自 HTTP/1.1 起)来解决此问题,该问题不允许更改 HTTP 方法 - 因此来自 POST 的重定向仍应为 POST - 请参阅维基百科:

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:

在这种情况下,应该使用另一个 URI 重复请求;但是,未来的请求仍应使用原始 URI.在与历史上 302 的实现方式相比,请求方法重新发出原始请求时不允许更改.为了例如,一个 POST 请求应该使用另一个 POST 重复请求.

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
}); 

为此:

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天全站免登陆