本篇教程介绍了Node.js如何实现重定向,希望阅读本篇文章以后大家有所收获,帮助大家对Node.js的理解更加深入。
<
一 方式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更简便,二者区别?
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注WEB前端Node.js频道!