Express is an implementation-based web framework for node. JS that response
has two objects in response to HTTP requests for URL jump methods res.location()
and res.redirect()
301 or 302 redirects for URLs that can be implemented using them.
Res.location (PATH)
Res.location (PATH)
Here are a few examples of how to set HTTP response header Location
Res.location ('/foo/bar '); Res.location (' http://example.com '); Res.location (' Back ');
The path value back has a special meaning, which involves the Referer
URL specified in the request header, ifReferer头没有指定,将会设置为‘/‘。
Express
The Location
specified URL string is passed to the browser by the header, and it does not validate (in addition to) the specified string ‘back‘
. The browser is responsible for redefining the current URL to Location
the URL specified in the response header.
Res.redirect ([Status,] path)
Where parameters:
status
: {Number}, indicating the HTTP status code to set
path
: {String}, to be set to the Location
URL in the header
Redirects to the specified URL using the specified HTTP status code, if no HTTP status code is specified, using the default status code "302": "Found",
Res.redirect ('/foo/bar '); Res.redirect (' http://example.com '); Res.redirect (301, ' http://example.com '); Res.redirect ('.. /login ');
Redirection can be a full URL, which redirects to a different site.
Res.redirect (' http://google.com ');
Redirects can also be relative to the host's root directory, for example, if your program is running on: http://example.com/admin/post/new The following code will be redirected to the following address: Http://example.com/admin
Res.redirect ('/admin ');
Redirection can also be relative to the current URL, for example: from the http://example.com/blog/admin/address (note the backslash), the following code will be redirected to the address:http://example.com/blog/admin/post/new
Res.redirect (' post/new ')
In From address:http://example.com/blog/admin重定向到 post/new,如果没有反斜杠的话将会重定向到:http://example.com/blog/post/new
If you feel confused about the behavior above, think about the file directory and the path to the file, which will give you a better understanding.
relative path redirection is also allowed if your address is:http://example.com/admin/post/new,下面的代码将会重定向到http//example.com/admin/post这个地址:
Res.redirect ('.. ');
back重定向,重定向到请求的referer,当没有referer请求头的情况下,默认为‘/’
URL redirection principle
When URL redirection occurs, the server only sets the HTTP状态码
and header information in the HTTP header information for the response information Location
.
When the status code is 301
either 302
( 301
-Permanent redirect, 302
-temporary redirect), the resource location changes and redirection is required.
Location
The header information represents the location of the resource change, that is, the URL to jump redirect.
location()
And
redirect()
The comparison
Express
response
object that is an extension to the native object ServerResponse
of node. js. location()
method only sets Location
the header, and the redirect()
method can be set automatically or at hand, in addition to setting the Location
header HTTP状态码
. In theory both can achieve redirection.
location()
The method implementation process is generally as follows:
res.location = function (URL) { var req = this.req; "Back" is Referrer's alias if (' back ' = = URL) url = req.get (' Referrer ') | | '/'; Set lcation this.setheader (' location ', url); return this;};
As can be seen from the above code, the location()
method is essentially a method of invoking an ServerResponse
object setHeader()
, but does not have a status code set. location()
after the header information is set, the code that follows is also executed.
Use location()
the method to implement the redirection of the URL, and also manually set HTTP状态码
:
Res.location (' http://itbilu.com '); res.statuscode = 301;
If you need to return the response information immediately, also call the end()
method:
Res.location (' http://itbilu.com '); res.statuscode = 301;res.end (' content of the response ');//or Res.location (' http://itbilu.com '); Res.sent (302);
redirect()
The method implementation process is generally as follows:
Res.redirect = function (URL) { var head = ' head ' = = This.req.method; var status = 302; var body; Some treatment ... Set the header information this.location (URL) through the location method; Other treatments ... Sets the state and returns the response This.statuscode = status; This.set (' Content-length ', Buffer.bytelength (body)); This.end (head null:body);};
从以上代码可以看出,redirect()
方法是对location()
方法的扩展。通过location()
设置Loction
头后,设置HTTP状态码
,最后通过ServerResponse
对象的end()
方法返回响应信息。调用redirect()
方法后,其后的代码都不会被执行
Redirect and no redirect
In the process of use, redirect()
most methods can be redirected successfully, but the location()
method is not very certain, sometimes success sometimes can not succeed. This is related to our usage.
As mentioned above, URL redirection is done on the browser side, and URL redirection is related to HTTP状态码
the Location
header. The browser will first determine the status code, only if the status code is: 301
or 302
, it will be based on Location
the URL in the header to jump.
Therefore, using the location()
set header information instead of setting the status code or the status code is not 301
302
, and redirects do not occur:
Res.location (' http://itbilu.com '); res.sent (200);
Instead redirect()
of using the Set status code, the jump is not 301
or will 302
occur:
Res.redirect (+, ' http://itbilu.com ');
Reference:
1, http://itbilu.com
2, Http://www.expressjs.com.cn/4x/api.html#res.location
Express URL Jump (redirect) implementation