EmberJS Mirage 404获取/租赁错误

时间:2016-09-02 07:32:29

标签: ember.js http-status-code-404 ember-cli-mirage

我正在关注此页面上的教程(https://guides.emberjs.com/v2.7.0/tutorial/installing-addons/),直到下一个教程页面。 (实际上我一直关注,直到完成教程。)

一切似乎都很好。 ✔
Ember服务器正在服务并在浏览器中正确显示。 ✔
Ember开发版本也能正确显示。 ✔

但Ember生产版本给我 / rental 404错误。 ✖
如何在生成版本上修复此404错误?

这是我的 mirage / config.js

export default function() {

  // These comments are here to help you get started. Feel free to delete them.

  /*
    Config (with defaults).

    Note: these only affect routes defined *after* them!
  */

  // this.urlPrefix = '';    // make this `http://localhost:8080`, for example, if your API is on a different server
  // this.namespace = '';    // make this `api`, for example, if your API is namespaced
  // this.timing = 400;      // delay for each request, automatically set to 0 during testing

  /*
    Shorthand cheatsheet:

    this.get('/posts');
    this.post('/posts');
    this.get('/posts/:id');
    this.put('/posts/:id'); // or this.patch
    this.del('/posts/:id');

    http://www.ember-cli-mirage.com/docs/v0.2.x/shorthands/
  */

  this.get('/rentals', function(db, request) {
    let rentals = [{
        type: 'rentals',
        id: 1,
        attributes: {
          title: 'Grand Old Mansion',
          owner: 'Veruca Salt',
          city: 'San Francisco',
          type: 'Estate',
          bedrooms: 15,
          image: 'https://upload.wikimedia.org/wikipedia/commons/c/cb/Crane_estate_(5).jpg'
        }
      }, {
        type: 'rentals',
        id: 2,
        attributes: {
          title: 'Urban Living',
          owner: 'Mike Teavee',
          city: 'Seattle',
          type: 'Condo',
          bedrooms: 1,
          image: 'https://upload.wikimedia.org/wikipedia/commons/0/0e/Alfonso_13_Highrise_Tegucigalpa.jpg'
        }
      }, {
        type: 'rentals',
        id: 3,
        attributes: {
          title: 'Downtown Charm',
          owner: 'Violet Beauregarde',
          city: 'Portland',
          type: 'Apartment',
          bedrooms: 3,
          image: 'https://upload.wikimedia.org/wikipedia/commons/f/f7/Wheeldon_Apartment_Building_-_Portland_Oregon.jpg'
        }
      }];

    if(request.queryParams.city !== undefined) {
      let filteredRentals = rentals.filter(function(i) {
        return i.attributes.city.toLowerCase().indexOf(request.queryParams.city.toLowerCase()) !== -1;
      });
      return { data: filteredRentals };
    } else {
      return { data: rentals };
    }
  });

}

网址前缀和命名空间不会更改任何内容,仍然/出租404错误。

  

获取http://localhost/rentals 404未找到
  " NetworkError:404 Not Found - http://localhost/rentals"
  处理路线时出错:索引Ember数据请求GET /租金返回404

2 个答案:

答案 0 :(得分:3)

在生产版本中禁用了

ember-cli-mirage,您应该在config中明确启用它:

if (environment === 'production') {
  ENV['ember-cli-mirage'] = {
    enabled: true
  };
}

Mirage Documentation

答案 1 :(得分:-1)

文档已更新,包括针对此问题的修复程序。

现在,如果转到文档Setting up Application Tests to use Mirage中的部分,您会看到它说打开/tests/acceptance/list-rentals-test.js并在顶部的此import语句中插入:

import setupMirage from 'ember-cli-mirage/test-support/setup-mirage';

然后在tests/acceptance/list-rentals-test.js中添加setupMirage(hooks);之后的setupApplicationTest(hooks)

module('Acceptance | list rentals', function(hooks) {
  setupApplicationTest(hooks);
  setupMirage(hooks);
  ...
}

然后数据和测试将通过。

相关问题