Node Express不是redirect,而是返回一个string

我是新来的节点和angular度,试图找出如何redirect一个错误的请求。 节点本质上是一个RESTful API的代理。 目前,weather.error正在填充为“ Bad Request. Redirecting to /public/400.html Bad Request. Redirecting to /public/400.html

服务器调用:

 app.get('/currently', function(request, response) { var options = { exclude: 'minutely,hourly,daily,flags,alerts' }; forecast.get(request.query.latitude, request.query.longitude, options, function(err, res, data) { if (err) { console.log('Error in request. \nLatitude: ' + request.query.latitude + '\nLongitude: ' + request.query.longitude); var status = data.split('<title>')[1].split(' ')[0] if (status === '404') { response.status(400).sendFile(__dirname + '/public/images/404/404-' + Math.floor((Math.random() * 3) + 1) + '.jpg'); } else if (status === '400') { response.redirect(400, '/public/400.html'); } else { response.status(parseInt(status)).end(); } } else { response.send(JSON.stringify(data)); } }); }); 

控制器:

 angular.module('myApp.weather', [ 'ui.router' ]) .config(function($stateProvider) { $stateProvider .state('weather', { url: '/weather', templateUrl: 'app/weather/weather.tmpl.html', controller: 'WeatherController as weather' }); }).controller('WeatherController', function($http, $location) { var weather = this; weather.latitude = ''; weather.longitude = ''; weather.getCurrentWeatherByLatLon = function(latitude, longitude) { $http.get('/currently?latitude=' + latitude + '&longitude=' + longitude) .success(function(data, status, headers, config) { weather.data = data; }) .error(function(data, status, headers, config) {//I don't think this should be necessary if I'm handling it in the sever weather.error = data; if(data.indexOf('Bad Request') >= 0) { console.log('Bad Request') $location.path('/public/400.html'); //doesn't navigate anywhere } }) ; }; 

– – – – – – – – – – – – – – – -编辑 – – – – – – – – – ——-

我已将我的服务器调用更改为:

 app.get('/currently', function(request, response) { var options = { exclude: 'minutely,hourly,daily,flags,alerts' }; forecast.get(request.query.latitude, request.query.longitude, options, function(err, res, data) { if (err) { console.log('Error in request. \nLatitude: ' + request.query.latitude + '\nLongitude: ' + request.query.longitude); var status = data.split('<title>')[1].split(' ')[0] response.status(status).send(); } else { response.send(JSON.stringify(data)); } }); }); 

和我的控制器中的function:

 weather.getCurrentWeatherByLatLon = function(latitude, longitude) { $http.get('/currently?latitude=' + latitude + '&longitude=' + longitude) .success(function(data, status, headers, config) { weather.data = data; // this callback will be called asynchronously // when the response is available }) .error(function(data, status, headers, config) { weather.error = status if (status == 400) { console.log('in 400'); $location.url('/public/400.html'); } else if (status == 404) { console.log('in 404'); $location.url('/public/404.html'); } }); }; 

我build议你使用locationProvider服务的url方法,这个许可证将stream量的控制权交给一个控制器pipe理的路由:是的, 你必须通过一个控制器来实现这个路由

尝试

 $location.url('/public/400.html'); 

代替

 $location.path('/public/400.html'); 

服务静态内容的另一个select是使用window服务:

尝试

 $window.location.href ='/public/400.html'; 

代替

 $location.path('/public/400.html'); 

有益于使用这个而不是标准的JavaScript窗口对象( 从文档 ):

对浏览器窗口对象的引用。 尽pipeJavaScript在全球范围内可用,但它会引起可testing性问题,因为它是一个全局variables。 在angular度上,我们总是通过$ window服务来引用它,所以它可能会被覆盖,删除或模拟进行testing。

希望这个帮助

你不能redirectAjax调用。 使用response.redirect(400, '/public/400.html')进行的redirect只是一个带有“特殊”头部的HTTP响应,并且不受ajax支持。

如果您不想处理请求中的问题,请返回状态或类似地在客户端上处理它。