【问题标题】:Browser is not getting redirected with res.sendFile()浏览器没有被 res.sendFile() 重定向
【发布时间】:2015-08-19 18:40:00
【问题描述】:

在我的 Express 应用中,当管理员成功登录时,我希望他被发送到他的仪表板页面。所以在post 函数中,如果成功,我会重定向到admin/,这会将他带到下一个函数

所以我有:

adminRoutes.get('/', function(req, res){
    console.log("/", path.join(__dirname, 'public/admin/index.html'));

    res.sendFile(path.join(__dirname, 'public/admin/index.html'), {}, function (err) {
        if (err) {
          console.log(err);
          res.status(err.status).end();
      }
      else {
          console.log('Sent:');
      }
  });
});

但问题是,我的浏览器没有任何反应。它只是停留在浏览器页面上。它不会更改为我正在发送的新页面。

为什么它不起作用?

【问题讨论】:

  • 您是否正在控制台中访问日志“Sent:”?
  • @laggingreflex 是的,我是。这就是困扰我的原因

标签: node.js redirect express sendfile


【解决方案1】:

您应该使用redirect 而不是sendFilesendFile 用于告诉浏览器下载特定文件。试试:

res.redirect('public/admin');

另外,请确保这已在您的路线中注册。然后路由应该render HTML。

【讨论】:

  • public/admin 的路线上有什么?在某个时候,我必须发送一个文件
  • 您是指可下载的文件吗?还是呈现为 HTML 的文件?对于浏览器来说,这是两个完全不同的概念
猜你喜欢
  • 1970-01-01
  • 2016-10-21
  • 1970-01-01
  • 2013-09-24
  • 1970-01-01
  • 2015-05-31
  • 2016-05-01
  • 1970-01-01
  • 2013-07-26
相关资源
最近更新 更多