301/302 redirection and implementation of nodejs

There are two types of URL redirection: 301 and 302. Both 301 and 302 are HTTP status codes, which represent a change in a URL. But the difference is:

  1. 301

    Permanent redirection means that the current webpage is permanently transferred to another URL. When the search engine fetches new content, the old URL will be replaced with the redirected URL, and the original external links under the old URL will be transferred to Under the new address, so as not to affect the ranking of the current website. That is: the new URL completely inherits the old URL, and the ranking of the old URL is completely cleared. In the actual scenario: For example, when you visit a.com, b.com will be redirected. When you visit a.com next time, the browser will not initiate a request for a.com but directly visit b.com. That is, the browser will remember

  2. 302

    Temporary redirection means that the current webpage is temporarily transferred to another URL, and the search engine will crawl the new content but retain the old URL. That is, it has no effect on the old URL, and the new URL will not have a ranking. In actual scenarios: For example, the browser does not remember, and will visit the old address first every time.

How to make the client redirect through nodejs?

  1. The status code is set to 302 to indicate temporary redirection

    statusCode

  2. Tell the client where to redirect through Location in the response header

    setHeader

  3. If the client finds that the status code of the response from the server is 302, it will automatically find Location in the response header, and then initiate a new request to the address

  4. You can view the redirection process in the developer tools, check ->Preserve log (do not let it clear the previous request response records)

  5. Case:

    let http = require('http')
    http
      .createServer((req,res) => {
          
          
    	// 重定向
    	res.statsuCode = 302;// 302重定向
       // res.statusCode = 301;// 301重定向
    	res.setHeader('Location','/index.html');
    	res.end();
    })
      .listen(3000,()=>{
          
          
    	console.log('server is running...');
    })
    
    

Guess you like

Origin blog.csdn.net/chen__cheng/article/details/114645807