【问题标题】:unable to serve json file with express.static无法使用 express.static 提供 json 文件
【发布时间】:2013-08-02 13:36:17
【问题描述】:

我有几个问题:

我正在使用 angular 和 node (Express) 构建一个应用程序。

1) 在我的节点服务器中,我正在为我的 static_dir 中的所有静态文件提供服务

app.use(express.static(STATIC_DIR));

insdie 这个目录我有一个 json 文件 static_Dir/courts/courts.json

我的控制如下:

squashApp.controller('CourtsController',
    function CourtsController($scope) {
    //$http.get('courts/courts.json').success(function(data) {
        $scope.courts = 'courts/courts.json';
  });

我的 html 文件是:

<table class="table">
    <tr>
      <th>Hour</th>
      <th>Player 1</th>
      <th>Player 2</th>
    </tr>
    <tr>
      <td>itai</td>
      <td>asf</td>
      <td>sdf</td>
    </tr>
    <tr ng-repeat="court in courts">
      <td>{{court.hour}}</td>
      <td>{{court.player1}}</td>
      <td>{{court.player2}}</td>
    </tr>

由于某种原因,这个 json 文件的信息没有显示在我的表格中。 是什么原因?

关于单元测试和 jasmin 的另一个问题:

我的测试:

describe('controllers', function(){

  it('should create courts model', (function() {
    var scope = {},
    ctrl = new squashApp.controller.CourtsController(scope);
    expect(scope.courts.length).toBe(6)
  }));

});

但我得到一个错误,即 CourtsController 未定义。 为了使测试正常工作,正确的语法应该是什么。

谢谢。

【问题讨论】:

  • 角度处理文件系统是否为您读取?

标签: javascript node.js angularjs express


【解决方案1】:
var path = require('path');
...
app.use(express.static(path.join(__dirname, 'public')));

/courts/courts.json

【讨论】:

  • 这并没有真正解决任何问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 1970-01-01
  • 2016-10-13
  • 1970-01-01
相关资源
最近更新 更多