码迷,mamicode.com
首页 > Web开发 > 详细

nodejs 重定向 (redirect + writeHead(Location))

时间:2017-03-29 12:26:42      阅读:1183      评论:0      收藏:0      [点我收藏+]

标签:strong   target   get   exp   dex   write   cat   function   expr   

参考:

Node.js实现301、302重定向服务

Express URL跳转(重定向)的实现:res.location()与res.redirect()

 

一 方式1

index.js

var http = require(‘http‘);

var server = http.createServer(function (req, res) {
    res.writeHead(301, {‘Location‘: ‘http://itbilu.com/‘});
    console.log(res._header);
    res.end();
});

server.listen(3000);

浏览器打开http://127.0.0.1:3000,页面跳转到http://itbilu.com。

 

一 方式2

index.js

var http = require(‘http‘);

var server = http.createServer(function (req, res) {

res.redirect(‘http://itbilu.com/‘); 
//res.redirect(301,‘http://itbilu.com/‘);
res.end(); }); server.listen(3000);

res.redirect更简便,二者区别?

 

nodejs 重定向 (redirect + writeHead(Location))

标签:strong   target   get   exp   dex   write   cat   function   expr   

原文地址:http://www.cnblogs.com/gamedaybyday/p/6639077.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!