如何在Node.js/Express中进行重定向(301)? [英] How to make a redirect (301) in Node.js / Express?

查看:318
本文介绍了如何在Node.js/Express中进行重定向(301)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态网站,这是我在Dokku中部署的简单单页面布局.我需要从www到非www以及从*.website1.comwebsite2.com进行重定向(301),但是我不知道该怎么做.我试图在npm中使用express-force-domain,但它使我的URL变得疯狂,并出现了一些重定向循环,我正在尝试在网络上找到的其他方法,但没有成功.

I have a static site, a simple single page layout that I deploy in Dokku. I need to make a redirect (301) from www to non www and from *.website1.com to website2.com, but I don't know how. I was trying to use express-force-domain in npm but it makes my URL's crazy and having some redirect loops, I was trying other methods that I find on the web but none succeeds.

我的server.js是:

my server.js is:

var express = require('express')
var app = express();

app.set('port', (process.env.PORT || 80))
app.use(express.static(__dirname + '/public'))

app.get('/', function(request, response) {
  response.send('Hello World!')
})

app.listen(app.get('port'), function() {
  console.log("Node app is running at localhost:" + app.get('port'))
})

那是可行的版本,但是没有重定向.

That is the version that works, but without redirects.

推荐答案

res.redirect(301, 'http://yourotherdomain.com' + req.path)

请参见快递文档.

这篇关于如何在Node.js/Express中进行重定向(301)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆