如何在nodejs中redirect一个页面

目前这是行不通的。 我是新的nodejs,所以也许格式是完全错误的

function blueScreen(showit) { console.log(showit); if (showit) { function getFormattedUrl(req) { return url.format({ protocol: req.protocol, host: req.get('localhost:3000/blue.html') }); } } return res.redirect(getFormattedUrl(req));; } 

我知道这进入这个function,但屏幕不会改变showIt时是真的更新:res.redirect我相信是错误的,因为我从来没有定义rec。 不过,我会说,这是不是在同一个文件的应用程序是由于我的文件交互方式不能在该文件中。 有没有一种方法,我可以redirect我的屏幕在一个文件没有要求和RESfunction?

我假设你使用expressjs 。 基本上你有一个中间件function,你可以访问请求响应对象

 app.get('/blue', function (req, res) { res.redirect('/green'); });