如何使用新的余烬-CLI HTTP-模拟的API调用 [英] How to use the new ember-cli http-mock for API calls

查看:134
本文介绍了如何使用新的余烬-CLI HTTP-模拟的API调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单(到目前为止)烬-CLI项目,而现在只是有灯具的数据一个模型。我想小样API的东西,无论是与实际的JSON文件,或者用 HTTP-模拟,这是余烬,CLI版本41名曾经被认为是 API-存根

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.

我是pretty新的这一切,所以我真的不知道该让我发现信息里的人都能够得到一个什么样 API-存根工作,它看起来并不像href=\"http://ember-cli.com\">烬-CLI 已经更新了基于HTTP的信息呢。

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.

我做烬产生HTTP的模拟项目,但我不知道真的是从这里做。

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.

再次我还在学习我周围的余烬和灰烬-CLI方式,所以为什么和怎么样是非常AP preciated。

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

推荐答案

我是相当新的余烬/烬-CLI很好,但我有一个简单的HTTP-模拟原型工作。生成您HTTP的模拟项目后:

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

发电机应该已经创建了一个服务器文件夹中的项目与在嘲笑子目录中project.js模拟。如果您打开该文件(服务器/嘲笑/ 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

和curl'ing你的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'}}

这篇关于如何使用新的余烬-CLI HTTP-模拟的API调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆