获取 /rentals 时出现 EmberJS Mirage 404 错误
EmberJS Mirage 404 error on get /rentals
我正在学习此页面 (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 Not Found
"NetworkError: 404 Not Found - http://localhost/rentals"
Error while processing route: index Ember Data Request GET /rentals returned a 404
ember-cli-mirage 在生产版本中被禁用,您应该在配置中明确启用它:
if (environment === 'production') {
ENV['ember-cli-mirage'] = {
enabled: true
};
}
文档已更新,包含针对此问题的修复程序。
现在,如果您转到文档中名为 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);
...
}
那么数据和测试就会通过。
我正在学习此页面 (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 Not Found
"NetworkError: 404 Not Found - http://localhost/rentals"
Error while processing route: index Ember Data Request GET /rentals returned a 404
ember-cli-mirage 在生产版本中被禁用,您应该在配置中明确启用它:
if (environment === 'production') {
ENV['ember-cli-mirage'] = {
enabled: true
};
}
文档已更新,包含针对此问题的修复程序。
现在,如果您转到文档中名为 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);
...
}
那么数据和测试就会通过。