简体   繁体   English

如何使用angular.js,ui-router和node.js刷新路由

[英]How can I refresh routes with angular.js, ui-router, and node.js

I have an angularjs and nodejs app with ui-router that works well from the home page. 我有一个带有ui-router的angularjs和nodejs应用程序,可以从主页正常运行。 The problem is I cannot refresh any other state or type another state url in the browser and go directly to it. 问题是我无法刷新任何其他状态或在浏览器中键入其他状态url并直接转到它。

I have nodejs respond to requests for the state urls with the index.html file 我让nodejs用index.html文件响应对状态url的请求

app.get('/*', function (req, res) {
  res.sendfile('client/index.html');
});

The app gets index.html and requests all the scripts from index.html but for some reason it adds the state url in front of the request. 该应用程序获取index.html并从index.html请求所有脚本,但是由于某种原因,它在请求的前面添加了状态url。 For instance in my index.html header there is this script 例如在我的index.html标头中有此脚本

<link href="css/normalize.css" rel="stylesheet">

If I refresh the browser from a state other than home, then the script is requested like this: 如果我从家庭以外的其他状态刷新浏览器,则要求脚本如下:

GET /albums/css/normalize.css

edit: I fixed this problem ^^ by adding a '/' in the link like so: href="/css/normalize.css". 编辑:我通过在链接中添加“ /”来解决此问题^^,例如:href =“ / css / normalize.css”。 Now the index file loads but angular does not send a get request for the partial file associated with the state. 现在,索引文件已加载,但angular不会发送与该状态关联的部分文件的获取请求。 It only loads the index.html file. 它仅加载index.html文件。

This is my app.js file for angular 这是我的app.js文件

angular.module('myApp', [
    'ngTouch',
    'ui.router',
    'ngRoute',
    'ngAnimate',
    'myApp.controllers',
    'myApp.directives',
    'myApp.restServices',
    'ui.bootstrap',
    'ngCookies',
    'ngResource',
    'ngSanitize',
    'snap',
    'angular.css.injector'
]).
config(['$stateProvider', '$httpProvider', '$urlRouterProvider', '$locationProvider','snapRemoteProvider',  function ($stateProvider, $httpProvider, $urlRouterProvider, $locationProvider, snapRemoteProvider) {
    snapRemoteProvider.globalOptions.disable = 'right';
    $httpProvider.interceptors.push('authInterceptor');
    $urlRouterProvider.otherwise("home");

    $stateProvider
      .state('home', {url: "/",templateUrl: "partials/home.html",controller: 'homeCtrl'})
      .state('albums', {url: "/albums",templateUrl: "partials/albums/albums.html",controller: 'albumsCtrl'})

.config(['$locationProvider', function ($locationProvider) {
  $locationProvider.html5Mode(true);
  $locationProvider.hashPrefix('!');
}]);

This is my node server 这是我的节点服务器

var express = require('express'),
    url = require('url');
    path = require('path'),
    bodyParser = require('body-parser'),
    directory = require('./routes/directory'),
    app = express();

app.use(function (req, res, next) {
    res.setHeader('Access-Control-Allow-Origin', 'http://curtwphillips.com');
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With, Accept, X-api-key, X-auth-token, Content-Type, Content-Length');
    res.setHeader('Access-Control-Allow-Credentials', true);
    if (req.headers && req.headers.authorization) { delete req.headers.authorization; }
    next();
});
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static(path.join(__dirname, '/client')));
app.use(function(req, res, next){
  console.log('%s %s', req.method, req.url);
  next();
});

app.get('/*', function (req, res) {
  res.sendfile('client/index.html');
});


app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {

});

I figured out the problem. 我解决了这个问题。 I was naming some of my states with the same names as folders in my client folder. 我在命名某些状态时使用与客户端文件夹中的文件夹相同的名称。

When I tried to refresh the 'albums' state, shown here: 当我尝试刷新“专辑”状态时,显示如下:

.state('albums', {url: "/albums",templateUrl: "partials/albums/albums.html",controller: 'albumsCtrl'})

the url "/albums" was matched to my "/client/albums" directory by this node code 该节点代码将URL“ /相册”与我的“ / client / albums”目录匹配

app.use(express.static(path.join(__dirname, '/client')));

I fixed the issue by renaming my folders so they don't match the angular urls. 我通过重命名文件夹来解决该问题,以使它们与角度网址不匹配。 This way the catchall code sends index.html. 这样,全部代码将发送index.html。 Hopefully this helps someone out there. 希望这可以帮助某个人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM