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

问题描述

我有一个简单(到目前为止)的ember-cli项目,现在只有一个模型与FIXTURE数据。我想使用实际的JSON文件,或使用 http-mock 来模拟API的东西,这是以前的$ $的ember-cli版本41的名称c $ c> api-stub 。



我对这一切很新鲜,所以我真的不知道该怎么做我发现这些信息可以让人们得到 api-stub 的工作,并且它看起来不像任何文档在已更新为 http-mock 信息。



我做了 ember生成http-mock项目,但我不知道从这里做什么。



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

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

所以我有一个所有项目的模板,并希望深入到一个,这需要能够与嵌套的路由相关联。
我的理想将是,您可以在其中深入了解用户再次,我还在学习我的方式,在ember和ember-cli,所以解释为什么为什么作为一个回购,对该回购的问题,等等。



解决方案

我相当新的ember / ember-cli,但我得到一个简单的http-mock原型工作。生成您的http-mock项目后:

 > ember g http-mock项目

生成器应该在你的项目中创建一个'server'文件夹,你的project.js模拟在'mocks'子目录中。如果你打开那个文件(server / mocks / project.js),你应该看到这样的东西:

  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);
};

您将要使用json更新res.send(...)回应。例如:

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

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

 > ember服务器

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

 > curl -HContentType:application / jsonhttp: / localhost:4200 / api / project 

应该回复:

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


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.

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.

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

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');
  });
});

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.

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

解决方案

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

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);
};

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

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

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

Should respond with:

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

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

10-22 02:22