本文介绍了如何使用新的 ember-cli http-mock 进行 API 调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的(到目前为止)ember-cli 项目,现在只有一个带有 FIXTURE 数据的模型.我想模拟 API 的东西,要么用实际的 JSON 文件,要么用 http-mock,这是过去 api-stub.

I have a simple (so far) ember-cli project, and right now just have one model with FIXTURE data. I would like to mock up API stuff, either with actual JSON files, or with http-mock, which is the ember-cli version 41 name of what used to be api-stub.

我对这一切都很陌生,所以我真的不知道如何处理我在人们能够让 api-stub 工作的地方找到的信息,但它没有看起来 ember-cli 上的任何文档都已经更新了 http-mock 信息.

I'm pretty new to all this, so I really didn't know what to make of the info I found where people were able to get api-stub working, and it doesn't look like any docs on the ember-cli have been updated with http-mock info yet.

我确实做了 ember generate http-mock project 但我不确定从这里开始做什么.

I did do ember generate http-mock project but I'm not sure really what to do from here.

这是我当前的应用程序/router.js:

Here's my current app/router.js:

Router.map(function() {
  this.resource('projects', { path: '/' });
  this.resource('project', {path: '/project/:project_id'}, function(){
    this.resource('milestones');
    this.resource('team');
    this.resource('budget');
  });
});

所以我有一个模板用于我的所有项目,并希望深入到一个模板,它需要能够与嵌套路由相关联.我的理想是类似于 GitHub API,您可以在其中从用户深入到存储库,关于该回购的问题等.

So I have a template for all my projects, and want to drill down to one, which need to be able to relate to the nested routes.My ideal would be something like the GitHub API where you can drill down from a user to a repo, to issues on that repo, etc.

同样,我仍在学习 ember 和 ember-cli,因此非常感谢对为什么"和如何"的解释.

Again, I'm still learning my way around ember and ember-cli, so explanations of "why" as well as "how" are hugely appreciated.

推荐答案

我对 ember/ember-cli 也很陌生,但我得到了一个简单的 http-mock 原型.生成 http-mock 项目后:

I'm fairly new to ember/ember-cli as well but I got a simple http-mock prototype working. After generating your http-mock project:

>ember g http-mock project

生成器应该在您的项目中创建了一个server"文件夹,并在mocks"子目录中使用您的 project.js 模拟.如果您打开该文件 (server/mocks/project.js),您应该看到如下内容:

The generator should have created a 'server' folder within your project with your project.js mock in the 'mocks' subdirectory. If you open up that file (server/mocks/project.js) you should see something like this:

module.exports = function(app) {
  var express = require('express');
  var projectRouter = express.Router();
  projectRouter.get('/', function(req, res) {
    res.send({project:[]});
  });
  app.use('/api/project', projectRouter);
};

您需要使用 api 响应的 json 更新 res.send(...).例如:

You'll want to update the res.send(...) with the json your api should respond with. eg:

res.send({project:{id: 1, number: 123, name: 'Fooshnickins'}});

您可以通过运行服务器向自己证明这是可行的:

You can prove to yourself this works by running your server:

>ember server

并卷曲您的 api(注意内容类型):

And curl'ing your api (note the content type):

>curl -H "ContentType:application/json" http://localhost:4200/api/project

应该回复:

{project:{id: 1, number: 123, name: 'Fooshnickins'}}

这篇关于如何使用新的 ember-cli http-mock 进行 API 调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 02:22