【问题标题】:Redirecting to static page when HTTP method is not found on Node backend在 Node 后端找不到 HTTP 方法时重定向到静态页面
【发布时间】:2017-12-06 02:25:16
【问题描述】:

我有一个用 AngularJS 和 NodeJS 编写的单页应用程序。在客户端,用户可以导航到多个路径:

http://jabaridash.com/#!/home
http://jabaridash.com/#!/interests/travel

当用户键入不存在的路径(例如http://jabaridash.com/#!/foo)时,AngularJS 通过重新路由到/notFound 路径来处理它,然后该页面将用户重定向回主页。这是代码:

$urlRouterProvider.otherwise('/notFound');

当路径以#!/anyPath 开头时,此方法有效。但是,如果我输入 jabaridash.com/whatever,Angular 不会重新路由它。我不确定这是否与我使用 $stateProvider 和模块进行导航有关,或者我需要在后端处理这种类型的路径。我假设我需要在 Node 端处理它,因为我在 NodeJS 端确实有一个名为photography 的 REST 端点,可以通过jabaridash.com/photography 访问(没有#!)。此端点工作正常,但我没有设置的任何其他端点都会收到以下响应:

Cannot GET /something

这是意料之中的,因为那里没有端点。所以本质上,我如何让 NodeJS 重定向到我的 index.html。我提供静态页面的方式如下:

/**
 * @description Set up the server
 *
 * @param dir directory to serve the index.html from
 */
function setupServer(dir) {
  server.use(express.static(dir), function(req, res, next) {

    // Allow cross origin from any host
    res.setHeader('Access-Control-Allow-Origin', '*');

    next();
  });

  // Set up the photography REST endpoint
  photography_controller(server, dir);
}

我的摄影端点设置如下:

server.get('/photography', function(req, res) {

  var searchPath = '/client/modules/interests/photography/img/thumbnail/';

  // Send the list of files for use on client side
  res.send(fileList(dir + searchPath, searchPath));
});

是否有一种通用的方式告诉 Node 说...“如果没有为给定路径定义端点/HTTP 方法,则重定向到已知路径?”

【问题讨论】:

    标签: angularjs node.js rest http redirect


    【解决方案1】:

    下面的这个 sn-p 告诉 express 当路径以 /example 开头但在路由器中没有指定的处理程序时重定向到 not-found。其他不以/example 开头的路由不会重定向,因为它们一开始不会被路由到路由器中。

    const express = require('express');
    const app = express();
    
    // create a router for /example
    var router = express.Router()
    .get('/', function (req, res) {
      res.send('example home');
    })
    .post('/about', function (req, res) {
      res.send('About example');
    })
    /*  catch all middleware for routes starting
        with /example that redirects to /not-found */
    .use(function (req, res, next) {
      return res.redirect('/not-found');
    });
    
    // attach router
    app.use('/example', router);
    
    // create a not-found router handler
    app.get('/not-found', function (req, res) {
      res.send('Not found page');
    });
    
    app.listen(3000, () => console.log('App listening on port 3000'));
    

    【讨论】:

      【解决方案2】:

      在您的路线定义之后,立即执行以下操作:

      server.get('/photography', function(req, res) {
        //do something
      });
      
      // catch 404 and forward to error handler
          server.use(function(req, res, next) {
            var err = new Error('Not Found');
            err.status = 404;
            next(err);
          });
          //
          // error handler
          server.use(function(err, req, res, next) {
            res.locals.message = err.message;
            res.locals.error = req.app.get('env') === 'development' ? err : {};
      
            // render the error page
            res.status(err.status || 500);
            //render the page you want to navigate to. In this example, I navigate the user to google
            res.redirect('http://google.com');
          });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-07-13
        • 2018-11-26
        • 2019-02-14
        • 1970-01-01
        • 2021-01-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多