【问题标题】:Node.js not loading AngularJS viewsNode.js 不加载 AngularJS 视图
【发布时间】:2016-04-28 04:30:23
【问题描述】:

我正在开发一个这样的网络应用程序:

- project
--- app
------ controllers
------ views
------ app.js
--- public
------ assets
--------- css
--------- js
--------- img
--- index.html

... 在我的 index.html 中,我有基本的 html、链接、脚本标签。加载 Angular 应用程序的所有内容都运行良好。

但现在,我需要使用 Node.js 加载所有应用程序。

首先我将 index.html 移动到 public/ 并创建了一个新文件。

index.js (Node.js)

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

app.get('/', function(req, res) {
    res.sendFile(path.join(__dirname + '/index.html'));
});

app.listen(3000, function () {
    console.log('Example app listening on port 3000!');
});

运行我在浏览器控制台获得的 Node.js:

GET http://localhost:3000/app/views/layouts/public.html 404 (Not Found)

这就是我加载角度视图的方式

app.config(function($urlRouterProvider, stateHelperProvider) {

  $urlRouterProvider.otherwise('/');
  $urlRouterProvider.when('', '/');

  stateHelperProvider.state({
    name: 'public',
    title: 'Home',
    url: '/',
    controller: 'PublicCtrl',
    templateUrl: '/app/views/layouts/public.html',
    data: {
      requireLogin: false
    }
  })
  .state({
    name: 'private',
    controller: 'PrivateCtrl',
    templateUrl: '/app/views/layouts/private.html',
    data: {
      requireLogin: true
    },
    children: [
      {
        name: 'browse',
        title: 'Home',
        url: '/browse',
        templateUrl: '/app/views/browse/index.html',
        controller: 'BrowseCtrl'
      }
    ]
  });

});

【问题讨论】:

  • 我没有看到您指示 node.js 在哪里提供您的视图。
  • 我没有,我只是指示node.js到index.html。剩下的应该是继续工作,而不是。
  • 如果你不指示 node.js 提供它,它就不会。
  • 感谢@Olly,index.html 和静态文件可以正常工作。但路由器视图不是。
  • 对,我说的就是这些。

标签: angularjs node.js


【解决方案1】:

尝试使用

app.get('/', function(req, res) {
    res.sendFile(path.join(__dirname + '/index.html'));
});

这对我有用,我只将 app.use 用于静态内容(就像你一样),比如 css 和 js。

你也把它写成你的 index.html 在你的公共目录中

希望对你有帮助

【讨论】:

  • 在你的浏览器控制台中吗?听起来那是因为它找不到你的 CSS 东西。在公共文件的路径中,只尝试assets/blah 而不是./assets/blah。它现在还找到你的html文件吗? :)
  • 抱歉,静态文件有效,但知道我有这个:GET http://localhost:3000/app/views/layouts/public.html 404 (Not Found)
  • 我不确定你为什么需要前两行,它们只会让事情变得更复杂......至少从我所见。在应用程序/视图中查看可能是因为您的第二行而感到困惑。完全不知道 /layouts/ 来自哪里。这是在节点控制台还是浏览器控制台中?
  • 您为什么要查找 public.html?那是从哪里来的?您的 Index.html 页面正在加载吗?编辑:忽略我你只是包括一个视图。也许从 GET 请求中删除 /layouts/?
  • /layouts/ 来自角度路线...检查更新
猜你喜欢
  • 2017-11-20
  • 1970-01-01
  • 1970-01-01
  • 2016-05-22
  • 1970-01-01
  • 1970-01-01
  • 2017-01-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多