【问题标题】:EmberJS Mirage 404 error on get /rentalsEmberJS Mirage 404 错误获取 /rentals
【发布时间】:2017-01-10 05:39:27
【问题描述】:

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

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

但是 Ember 生产版本给我 /rentals 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 };
    }
  });

}

url 前缀和命名空间没有任何改变,仍然 /rentals 404 错误。

GET http://localhost/rentals 404 未找到
“网络错误:404 未找到 - http://localhost/rentals”
处理路由时出错:索引 Ember 数据请求 GET /rentals 返回 404

【问题讨论】:

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


    【解决方案1】:

    ember-cli-mirage 在生产版本中被禁用,您应该在配置中明确启用它:

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

    Mirage Documentation

    【讨论】:

      【解决方案2】:

      文档已更新,包含对此问题的修复。

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

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

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

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

      那么数据和测试就会通过。

      【讨论】:

      • 为什么我会为此收到-1?
      猜你喜欢
      • 2013-11-24
      • 1970-01-01
      • 1970-01-01
      • 2022-11-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      相关资源
      最近更新 更多