Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 在节点后端找不到HTTP方法时重定向到静态页面_Angularjs_Node.js_Rest_Http_Redirect - Fatal编程技术网

Angularjs 在节点后端找不到HTTP方法时重定向到静态页面

Angularjs 在节点后端找不到HTTP方法时重定向到静态页面,angularjs,node.js,rest,http,redirect,Angularjs,Node.js,Rest,Http,Redirect,我有一个用AngularJS和NodeJS编写的单页应用程序。在客户端,用户可以导航到多个路径: http://jabaridash.com/#!/home http://jabaridash.com/#!/interests/travel 当用户键入不存在的路径时,例如http://jabaridash.com/#!/foo,AngularJS通过重新路由到/notFound路径来处理它,然后该页面将用户重定向回主页。代码如下: $urlRouterProvider.otherwise('/

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

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

$urlRouterProvider.otherwise('/notFound');
当路径以
#开头时,此选项有效/任意路径
。但是,如果我要键入
jabaridash.com/which
,Angular不会重新路由它。我不确定这是否与我使用$stateProvider和模块导航有关,或者我需要在后端处理这种类型的路径。我假设我需要在节点端处理它,因为我在NodeJS端有一个名为
photography
setup的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));
});

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

当路径以
/example
开头,但路由器中没有指定的处理程序时,下面的代码片段告诉express重定向到
未找到
。其他不以
/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'));

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

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');
    });