【发布时间】: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