模拟后端角/咕嘟咕嘟应用 [英] Mock backend for Angular / Gulp app

查看:131
本文介绍了模拟后端角/咕嘟咕嘟应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想没有reling对实体后端提供JSON响应嘲笑更快发展后端。前台应用程序是一个角度应用程序,我们使用咕嘟咕嘟作为开发和构建工具。

I would like to mock the backend for quicker development by providing json response without reling on the real backend. The frontend app is an Angular app and we use Gulp as a development and build tool.

例如。有一个特定的API(... /用户ELECTRONIC / 123)返回一个静态JSON结果。

E.g. have a specific api (.../custumers/123) return a static json result.

有可能已经为此一饮而尽工具?

Is there perhaps already a gulp tool for this?

推荐答案

我去 JSON -server 一饮而尽,JSON-SRV 我认为有一些简单和快速设置的好处。

I went with json-server and gulp-json-srv which I think had some benefits of simplicity and quick setup.

gulpfile.js配置启动JSON服务器和代理使用一饮而尽模拟任务中的HTTP调用:

gulpfile.js config to start json-server and to proxy the http calls using a "gulp mock" task:

gulp.task('mock', ['connect-mock'], function () {
    jsonServer.start({
        data: 'db.json',
        port: 8087
    });
});

gulp.task('connect-mock', function () {
    connect.server({
        port: 8085,
        livereload: true,
        middleware: function (connect, o) {
            return [(function () {
                var url = require('url');
                var proxy = require('proxy-middleware');
                var options = url.parse('http://127.0.0.1:8087');
                options.route = '/v2';
                return proxy(options);
            })()];
        }
    });
});

db.json与嘲讽数据:

db.json with mocked data:

{
    "customers": [
        { "id": 1, "name": "Johnny B" },
        { "id": 2, "name": "Steve G" },
        { "id": 3, "name": "Glenn H" }
    ]

这篇关于模拟后端角/咕嘟咕嘟应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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