{"id":15718,"date":"2017-01-12T12:15:59","date_gmt":"2017-01-12T10:15:59","guid":{"rendered":"https:\/\/www.webcodegeeks.com\/?p=15718"},"modified":"2017-01-10T12:26:29","modified_gmt":"2017-01-10T10:26:29","slug":"setting-angular-2-mockbackend","status":"publish","type":"post","link":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/","title":{"rendered":"Setting Up Angular 2 MockBackend"},"content":{"rendered":"<p>I recently finished up on a client project developing one of the company\u2019s first Angular 2 applications, which would be the first such application to go to production. I was on a team that consisted of only two developers: one for the backend RESTful web services and myself on the frontend Angular 2 application.<\/p>\n<p>When we first were getting the project off the ground, I found that I needed access to the REST services before they were ready. I needed a good way to mock them up. In this situation, I set up a mock service for an Angular 2 application using <a href=\"https:\/\/angular.io\/docs\/ts\/latest\/api\/http\/testing\/index\/MockBackend-class.html\" target=\"_blank\">MockBackend<\/a>.<\/p>\n<p>In this article I show a step-by-step example of setting up a mock of RESTful web service APIs for an Angular 2 application using Angular\u2019s MockBackend.<\/p>\n<h2>Setup<\/h2>\n<p>Before we get started, let\u2019s make sure that we have everything we need to work with our example application.<\/p>\n<p>The tutorial uses Git as its versioning system. You can check for what version of Git you have installed by running. You can download and install Git from <a href=\"http:\/\/git-scm.com\/download\" target=\"_blank\">http:\/\/git-scm.com\/download<\/a>.<\/p>\n<pre class=\"brush:perl\">$ git --version<\/pre>\n<p>We are also using Node.js. You can download a Node.js installer from <a href=\"https:\/\/nodejs.org\/en\/download\/\" target=\"_blank\">https:\/\/nodejs.org\/en\/download\/<\/a>. You can check your version of Node.js to make sure everything installed smoothly by running the following command:<\/p>\n<pre class=\"brush:perl\">$ node --version<\/pre>\n<p>Now that we have those two pieces, let\u2019s clone the <code>angular-mockbackend<\/code> repository located at GitHub by using the following command:<\/p>\n<pre class=\"brush:perl\">$ git clone https:\/\/github.com\/TLein77\/angular-mockbackend.git<\/pre>\n<p>Change your current directory to the <code>angular-mockbackend<\/code>:<\/p>\n<pre class=\"brush:perl\">$ cd angular-mockbackend<\/pre>\n<p>The last part of the setup will be to install all the necessary Node Modules with the following command:<\/p>\n<pre class=\"brush:perl\">$ npm install<\/pre>\n<p>Once the install is complete everything should be set up to run the Angular application from where we are going to start. Run the following command to start the application:<\/p>\n<pre class=\"brush:perl\">$ npm start<\/pre>\n<p>Once the application is started, go to <a href=\"http:\/\/localhost:8181\">http:\/\/localhost:8181<\/a>\u00a0and you should see the start of Angular\u2019s Tour of Heroes tutorial application. I have used this as a starting point for this tutorial and more information can be found at <a href=\"https:\/\/angular.io\/docs\/ts\/latest\/tutorial\/\">https:\/\/angular.io\/docs\/ts\/latest\/tutorial\/<\/a>.<\/p>\n<h2>Angular in-memory-web-api<\/h2>\n<p>The code is currently set up to use <a href=\"https:\/\/github.com\/angular\/in-memory-web-api\" target=\"_blank\">Angular in-memory-web-api<\/a>\u00a0for mocking all the data and API calls. This is one way to mock our calls, but exists to support the Angular documentation. Because we are going to implement <code>MockBackend<\/code>, let\u2019s go ahead and pull out the code that is needed for the <code>in-memory-web-api<\/code>.<\/p>\n<p>First, let\u2019s remove the dependency from our <code>package.json<\/code>. The new dependency block will be:<\/p>\n<pre class=\"brush:perl\">json\r\n\"dependencies\": {\r\n    \"@angular\/common\": \"~2.3.0\",\r\n    \"@angular\/compiler\": \"~2.3.0\",\r\n    \"@angular\/core\": \"~2.3.0\",\r\n    \"@angular\/forms\": \"~2.3.0\",\r\n    \"@angular\/http\": \"~2.3.0\",\r\n    \"@angular\/platform-browser\": \"~2.3.0\",\r\n    \"@angular\/platform-browser-dynamic\": \"~2.3.0\",\r\n    \"@angular\/router\": \"~3.3.0\",\r\n    \"bootstrap\": \"3.3.7\",\r\n    \"core-js\": \"^2.4.1\",\r\n    \"font-awesome\": \"4.6.3\",\r\n    \"jquery\": \"^3.1.1\",\r\n    \"reflect-metadata\": \"^0.1.8\",\r\n    \"rxjs\": \"5.0.0-rc.4\",\r\n    \"zone.js\": \"^0.7.2\"\r\n  },<\/pre>\n<p>Next remove the <code>in-memory-data-service.ts<\/code> file from <code>src\/app<\/code>.<\/p>\n<p>In <code>src\/app\/app.module.ts<\/code>, we need to remove the import of the <code>InMemoryWebApiModule<\/code> and <code>InMemoryDataService<\/code>. We also need to remove <code>InMemoryWebApiModule.forRoot(InMemoryDataService<\/code> from the Module imports.<\/p>\n<p>When done, the <code>app.module.ts<\/code> should look like the following:<\/p>\n<pre class=\"brush:perl\">javascript\r\nimport {NgModule} from \"@angular\/core\";\r\nimport {BrowserModule} from \"@angular\/platform-browser\";\r\nimport {AppComponent} from \".\/app.component\";\r\nimport {FormsModule} from \"@angular\/forms\";\r\nimport {HeroDetailComponent} from \".\/hero-detail\/hero-detail.component\";\r\nimport {HeroesComponent} from \".\/heroes\/heroes.component\";\r\nimport {HeroService} from \".\/hero\/hero.service\";\r\nimport {DashboardComponent} from \".\/dashboard\/dashboard.component\";\r\nimport {AppRoutingModule} from \".\/app-routing.module\";\r\nimport {HttpModule} from \"@angular\/http\";\r\n\r\n@NgModule({\r\n    imports: [\r\n        BrowserModule,\r\n        FormsModule,\r\n        AppRoutingModule,\r\n        HttpModule\r\n    ],\r\n    declarations: [\r\n        AppComponent,\r\n        HeroesComponent,\r\n        HeroDetailComponent,\r\n        DashboardComponent,\r\n    ],\r\n    providers: [HeroService],\r\n    bootstrap: [AppComponent]\r\n})\r\nexport class AppModule {}<\/pre>\n<p>The last bit that needs changed is the URL for the API calls. The <code>in-memory-web-api<\/code> URLs for this app are specified as <code>api\/heroes<\/code> in <code>src\/app\/hero\/hero.service.ts<\/code>. By using <code>MockBackend<\/code> we can specify the URL to be what the real URL will be, or at least what we believe it will be. Let\u2019s change the <code>heroesUrl<\/code> to be the following:<\/p>\n<pre class=\"brush:perl\">private heroesUrl = 'http:\/\/localhost:8080\/heroes';<\/pre>\n<p>If we start our application again, everything appears to be fine, until we go the application in our browser. Our API URL isn\u2019t pointing to anything that is real at this point. Let\u2019s change that by setting up the <code>MockBackend<\/code>.<\/p>\n<h2>MockBackend Setup<\/h2>\n<p><code>MockBackend<\/code> is used specifically for testing the <code>Http<\/code> service. The class is used to override providers to other backends. So let\u2019s start by editing the <code>app.module.ts<\/code> so our applications will be using the <code>MockBackend<\/code> instead of the <code>Http<\/code> service.<\/p>\n<p>In <code>app.module.ts<\/code> we should have the following:<\/p>\n<pre class=\"brush:perl\">@NgModule({\r\n    imports: [\r\n        BrowserModule,\r\n        FormsModule,\r\n        AppRoutingModule,\r\n        HttpModule\r\n    ],\r\n    declarations: [\r\n        AppComponent,\r\n        HeroesComponent,\r\n        HeroDetailComponent,\r\n        DashboardComponent,\r\n    ],\r\n    providers: [HeroService],\r\n    bootstrap: [AppComponent]\r\n})\r\nexport class AppModule {}<\/pre>\n<p>First let\u2019s comment out the <code>HttpModule<\/code> in the imports array:<\/p>\n<pre class=\"brush:perl\">javascript\r\nimports: [\r\n    BrowserModule,\r\n    FormsModule,\r\n    AppRoutingModule,\r\n    \/\/HttpModule\r\n],<\/pre>\n<p>I am leaving the <code>HttpModule<\/code> in for the purpose that our app will not go to production with this set up. At some point we will want this to go to the real APIs for the application, and those will use the <code>Http<\/code> service provided by the <code>HttpModule<\/code>.<\/p>\n<p>Now we need to bring in <code>MockBackend<\/code> and <code>BaseRequestOptions<\/code> to setup our mock <code>Http<\/code> service. Add <code>MockBackend<\/code> and <code>BaseRequestOptions<\/code> to the providers array:<\/p>\n<pre class=\"brush:perl\">MockBackend,\r\nBaseRequestOptions<\/pre>\n<p>The following imports will also need to be added:<\/p>\n<pre class=\"brush:perl\">import {MockBackend} from \"@angular\/http\/testing\";\r\nimport {BaseRequestOptions} from \"@angular\/http\";<\/pre>\n<p>Next we want to define and set our mock <code>Http<\/code> service in our <code>providers<\/code> array. We specify that this is a <code>Provider<\/code> for <code>Http<\/code> that depends on <code>MockBackend<\/code> and <code>BaseRequestOptions<\/code>.<\/p>\n<p>Add the following to the providers array:<\/p>\n<pre class=\"brush:perl\">{\r\n    provide: Http,\r\n    deps: [MockBackend, BaseRequestOptions],\r\n    useFactory: (backend: MockBackend, options: BaseRequestOptions) =&gt; { return new Http(backend, options); }\r\n}<\/pre>\n<p>We also need to add the <code>Http<\/code> import:<\/p>\n<pre class=\"brush:perl\">import {BaseRequestOptions, Http} from \"@angular\/http\";<\/pre>\n<p>After all of that, the <code>AppModule<\/code> code will look like the following:<\/p>\n<pre class=\"brush:perl\">import {NgModule} from \"@angular\/core\";\r\nimport {BrowserModule} from \"@angular\/platform-browser\";\r\nimport {AppComponent} from \".\/app.component\";\r\nimport {FormsModule} from \"@angular\/forms\";\r\nimport {HeroDetailComponent} from \".\/hero-detail\/hero-detail.component\";\r\nimport {HeroesComponent} from \".\/heroes\/heroes.component\";\r\nimport {HeroService} from \".\/hero\/hero.service\";\r\nimport {DashboardComponent} from \".\/dashboard\/dashboard.component\";\r\nimport {AppRoutingModule} from \".\/app-routing.module\";\r\nimport {MockBackend} from \"@angular\/http\/testing\";\r\nimport {BaseRequestOptions, Http} from \"@angular\/http\";\r\n\/\/import {HttpModule} from \"@angular\/http\";\r\n\r\n@NgModule({\r\n    imports: [\r\n        BrowserModule,\r\n        FormsModule,\r\n        AppRoutingModule,\r\n        \/\/HttpModule\r\n    ],\r\n    declarations: [\r\n        AppComponent,\r\n        HeroesComponent,\r\n        HeroDetailComponent,\r\n        DashboardComponent,\r\n    ],\r\n    providers: [\r\n        HeroService,\r\n        MockBackend,\r\n        BaseRequestOptions,\r\n        {\r\n            provide: Http,\r\n            deps: [MockBackend, BaseRequestOptions],\r\n            useFactory: (backend: MockBackend, options: BaseRequestOptions) =&gt; { return new Http(backend, options); }\r\n        }\r\n    ],\r\n    bootstrap: [AppComponent]\r\n})\r\nexport class AppModule {}<\/pre>\n<h2>Create the MockBackend<\/h2>\n<p>The application is now all setup to use <code>MockBackend<\/code>. All <code>Http<\/code> service calls will now be intercepted by the <code>MockBackend<\/code> and processed.<\/p>\n<p>So let\u2019s now tell the <code>MockBackend<\/code> what to do when it gets the API calls.<\/p>\n<p>First, create a new directory within your app named <code>mock-backend<\/code>.<\/p>\n<pre class=\"brush:perl\">src\/app\/mock-backend<\/pre>\n<p>For my applications I like to keep my mock data in a separate file from the rest of the code. For this example create a <code>mock-heroes.ts<\/code>.<\/p>\n<pre class=\"brush:perl\">src\/app\/mock-backend\/mock-heroes.ts<\/pre>\n<p>Now all we need is some data. Place the following in <code>mock-heroes.ts<\/code>:<\/p>\n<pre class=\"brush:perl\">export const HEROES: any[] = [\r\n    {id: 11, name: 'Mr. Nice'},\r\n    {id: 12, name: 'Narco'},\r\n    {id: 13, name: 'Bombasto'},\r\n    {id: 14, name: 'Celeritas'},\r\n    {id: 15, name: 'Magneta'},\r\n    {id: 16, name: 'RubberMan'},\r\n    {id: 17, name: 'Dynama'},\r\n    {id: 18, name: 'Dr IQ'},\r\n    {id: 19, name: 'Magma'},\r\n    {id: 20, name: 'Tornado'}\r\n];<\/pre>\n<p>Now let\u2019s create the backend. Create <code>mock-backend.service.ts<\/code>.<\/p>\n<pre class=\"brush:perl\">src\/app\/mock-backend\/mock-backend.service.ts<\/pre>\n<p>I have labeled this as a service and will make this Injectable:<\/p>\n<pre class=\"brush:perl\">@Injectable()\r\nexport class MockBackendService {\r\n\r\n}<\/pre>\n<p>We start by adding a constructor for the class that will inject <code>MockBackend<\/code>:<\/p>\n<pre class=\"brush:perl\">constructor(\r\n\tprivate backend: MockBackend\r\n) {}<\/pre>\n<p>Now define a function called <code>start<\/code>. This will be how the mock backend is started for the application.<\/p>\n<pre class=\"brush:perl\">start(): void {\r\n\r\n}<\/pre>\n<p>Now we tell the backend what to do. We start by taking the <code>MockBackend<\/code> we injected and subscribing to its connections. Now we can look at each connection and tell what we want to do with the connection. I also supply the URL that we will be testing for and a regular expression to tell if we are passing a hero\u2019s id.<\/p>\n<pre class=\"brush:perl\">this.backend.connections.subscribe((c: MockConnection) =&gt; {\r\n\tconst URL = \"http:\/\/localhost:8080\/heroes\";\r\n\tlet heroesIdRegex = \/\\\/heroes\\\/([0-9]+)\/i;\r\n});<\/pre>\n<p>If we go back and look at our <code>hero.service.ts<\/code> in <code>src\/app\/hero\/<\/code>, we see that there are two methods that are calling our API. One to return a list of all the Heroes and one to get a specific Hero by id.<\/p>\n<p>Let\u2019s start by mocking up the API for the list of Heroes, that way we can see our mocked backend in action.<\/p>\n<p>In <code>mock-backend.service.ts<\/code>, let\u2019s look at our <code>MockConnection<\/code> and see if it is the API call for the list of heroes. We will do two things.<\/p>\n<pre class=\"brush:perl\">if (c.request.url === URL &amp;&amp; c.request.method === 0) {\r\n\r\n}<\/pre>\n<p>We check that the request URL does indeed match the Heroes URL, and we check that the request method is <code>GET<\/code>. If both of these are <code>true<\/code>, the we can return the list of Heroes.<\/p>\n<p>To do so we create a new <code>Response<\/code> with the JSON of the heroes in the body of the response. Then we respond using the MockConnection\u2019s <code>mockRespond<\/code>.<\/p>\n<pre class=\"brush:perl\">c.mockRespond(new Response(new ResponseOptions({\r\n\tbody: JSON.stringify(HEROES)\r\n})));<\/pre>\n<p>Our entire <code>start<\/code> function should now look like the following:<\/p>\n<pre class=\"brush:perl\">start(): void {\r\n    this.backend.connections.subscribe((c: MockConnection) =&gt; {\r\n        const URL = \"http:\/\/localhost:8080\/heroes\";\r\n        let heroesIdRegex = \/\\\/heroes\\\/([0-9]+)\/i;\r\n\r\n        if (c.request.url === URL &amp;&amp; c.request.method === 0) {\r\n            c.mockRespond(new Response(new ResponseOptions({\r\n                body: JSON.stringify(HEROES)\r\n            })));\r\n        }\r\n    });\r\n}<\/pre>\n<p>Our entire <code>mock-backend.service.ts<\/code> to this point will be:<\/p>\n<pre class=\"brush:perl\">import {Injectable} from \"@angular\/core\";\r\nimport {MockBackend, MockConnection} from \"@angular\/http\/testing\";\r\nimport {HEROES} from \".\/mock-heroes\";\r\nimport {ResponseOptions, Response} from \"@angular\/http\";\r\n\r\n@Injectable()\r\nexport class MockBackendService {\r\n    constructor(\r\n        private backend: MockBackend\r\n    ) {}\r\n\r\n    start(): void {\r\n        this.backend.connections.subscribe((c: MockConnection) =&gt; {\r\n            const URL = \"http:\/\/localhost:8080\/heroes\";\r\n            let heroesIdRegex = \/\\\/heroes\\\/([0-9]+)\/i;\r\n\r\n            if (c.request.url === URL &amp;&amp; c.request.method === 0) {\r\n                c.mockRespond(new Response(new ResponseOptions({\r\n                    body: JSON.stringify(HEROES)\r\n                })));\r\n            }\r\n        });\r\n    }\r\n}<\/pre>\n<h2>Use the Mocked Backend<\/h2>\n<p>We have our backend mocked up, but we haven\u2019t used it yet. For that we need to inject it into our application.<\/p>\n<p>Open <code>src\/app\/app.component.ts<\/code>. Let\u2019s add some code to have our mock backend start on the construction of the <code>AppComponent<\/code>.<\/p>\n<p>First let\u2019s add the constructor and insert our <code>MockBackendService<\/code>:<\/p>\n<pre class=\"brush:perl\">constructor(\r\nprivate mockBackendService: MockBackendService\r\n) {\r\n\r\n}<\/pre>\n<p>Now in our constructor we can go ahead and call start on the service:<\/p>\n<pre class=\"brush:perl\">this.mockBackendService.start();<\/pre>\n<p>We also need to tell Angular that the <code>MockBackendService<\/code> is a <code>provider<\/code>, so let\u2019s add that to our component. It will now look like this:<\/p>\n<pre class=\"brush:perl\">@Component({\r\n    selector: 'my-app',\r\n    templateUrl: 'app.component.html',\r\n    styleUrls: ['app.component.css'],\r\n    providers: [MockBackendService]\r\n})<\/pre>\n<p>Here is how the <code>AppComponent<\/code> should now look:<\/p>\n<pre class=\"brush:perl\">import {Component} from \"@angular\/core\";\r\nimport {MockBackendService} from \".\/mock-backend\/mock-backend.service\";\r\n\r\nimport '..\/..\/public\/css\/styles.css';\r\n\r\n@Component({\r\n    selector: 'my-app',\r\n    templateUrl: 'app.component.html',\r\n    styleUrls: ['app.component.css'],\r\n    providers: [MockBackendService]\r\n})\r\nexport class AppComponent {\r\n    title = 'Tour of Heroes';\r\n\r\n    constructor(\r\n        private mockBackendService: MockBackendService\r\n    ) {\r\n        this.mockBackendService.start();\r\n    }\r\n}<\/pre>\n<p>The last thing we will need to do is adjust what the <code>HeroService<\/code> is reading from the response to set as the <code>Hero[]<\/code>. In the <code>getHeroes()<\/code> and <code>getHero(id:number)<\/code> functions, change the following:<\/p>\n<pre class=\"brush:perl\">response.json().data<\/pre>\n<p>to<\/p>\n<pre class=\"brush:perl\">response.json()<\/pre>\n<p>Now when we run <code>$ npm start<\/code> and open the application at <a href=\"http:\/\/localhost:8181\">http:\/\/localhost:8181<\/a>,\u00a0we can see the dashboard displaying five heroes. If we click on a hero we still won\u2019t see their details though. Let\u2019s add that next.<\/p>\n<p>The process will be the same as mocking the <code>getHeroes()<\/code> function. We are just testing that it matches the <code>heroesIdRegex<\/code> and that the request method is <code>GET<\/code>. We then do a little more processing to return the correct Hero.<\/p>\n<p>Below is the completed <code>MockBackendService<\/code> including the <code>getHero<\/code> function:<\/p>\n<pre class=\"brush:perl\">import {Injectable} from \"@angular\/core\";\r\nimport {MockBackend, MockConnection} from \"@angular\/http\/testing\";\r\nimport {HEROES} from \".\/mock-heroes\";\r\nimport {ResponseOptions, Response} from \"@angular\/http\";\r\n\r\n@Injectable()\r\nexport class MockBackendService {\r\n    constructor(\r\n        private backend: MockBackend\r\n    ) {}\r\n\r\n    start(): void {\r\n        console.log('MockBackendService start');\r\n        this.backend.connections.subscribe((c: MockConnection) =&gt; {\r\n            console.log('mockConnection url:: ' + c.request.url);\r\n            const URL = \"http:\/\/localhost:8080\/heroes\";\r\n            let heroesIdRegex = \/\\\/heroes\\\/([0-9]+)\/i;\r\n\r\n\r\n            if (c.request.url === URL &amp;&amp; c.request.method === 0) {\r\n                console.log(JSON.stringify(HEROES));\r\n                c.mockRespond(new Response(new ResponseOptions({\r\n                    body: JSON.stringify(HEROES)\r\n                })));\r\n            } else if (c.request.url.match(heroesIdRegex) &amp;&amp; c.request.method === 0) {\r\n                let matches = HEROES.filter((hero) =&gt; {\r\n                    return hero.id === +(c.request.url.match(heroesIdRegex)[1])\r\n                });\r\n                c.mockRespond(new Response( new ResponseOptions({\r\n                    body: JSON.stringify(matches[0])\r\n                })));\r\n            }\r\n        });\r\n    }\r\n}<\/pre>\n<h2>Last Thoughts<\/h2>\n<p>Through this entire process we have set up a mock of our RESTful Webservice APIs. We can now use that mock to work on our application before the real APIs are ready. Once we are done with the mockup, we can then dispose of our provider for the <code>Http<\/code> service and switch back to using the <code>HttpModule<\/code>. Then we remove the provider from the <code>AppComponent<\/code> and the constructor and we are back to making the real API calls.<\/p>\n<p>This is just one example of using the <code>MockBackend<\/code>. We can and should also use this for implementation in our unit tests.<\/p>\n<p>Thanks for spending your time with me, and feel free to let me know of any questions.<\/p>\n<div class=\"attribution\">\n<table>\n<tbody>\n<tr>\n<td><span class=\"reference\">Reference: <\/span><\/td>\n<td><a href=\"https:\/\/keyholesoftware.com\/2017\/01\/09\/setting-up-angular-2-mockbackend\/\">Setting Up Angular 2 MockBackend<\/a> from our <a href=\"http:\/\/www.webcodegeeks.com\/join-us\/wcg\/\">WCG partner <\/a>Todd Leininger\u00a0at the <a href=\"http:\/\/keyholesoftware.com\/\">Keyhole Software<\/a> blog.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>I recently finished up on a client project developing one of the company\u2019s first Angular 2 applications, which would be the first such application to go to production. I was on a team that consisted of only two developers: one for the backend RESTful web services and myself on the frontend Angular 2 application. When &hellip;<\/p>\n","protected":false},"author":211,"featured_media":909,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-15718","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-angular-js"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Setting Up Angular 2 MockBackend - Web Code Geeks - 2026<\/title>\n<meta name=\"description\" content=\"I recently finished up on a client project developing one of the company\u2019s first Angular 2 applications, which would be the first such application to go\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Setting Up Angular 2 MockBackend - Web Code Geeks - 2026\" \/>\n<meta property=\"og:description\" content=\"I recently finished up on a client project developing one of the company\u2019s first Angular 2 applications, which would be the first such application to go\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/\" \/>\n<meta property=\"og:site_name\" content=\"Web Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/webcodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2017-01-12T10:15:59+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Todd Leininger\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@webcodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Todd Leininger\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"11 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/\"},\"author\":{\"name\":\"Todd Leininger\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/b8e1e46688809b001b4e21bfe9e39312\"},\"headline\":\"Setting Up Angular 2 MockBackend\",\"datePublished\":\"2017-01-12T10:15:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/\"},\"wordCount\":1407,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg\",\"articleSection\":[\"Angular.js\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/\",\"name\":\"Setting Up Angular 2 MockBackend - Web Code Geeks - 2026\",\"isPartOf\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg\",\"datePublished\":\"2017-01-12T10:15:59+00:00\",\"description\":\"I recently finished up on a client project developing one of the company\u2019s first Angular 2 applications, which would be the first such application to go\",\"breadcrumb\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/#primaryimage\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.webcodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"JavaScript\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/javascript\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Angular.js\",\"item\":\"https:\/\/www.webcodegeeks.com\/category\/javascript\/angular-js\/\"},{\"@type\":\"ListItem\",\"position\":4,\"name\":\"Setting Up Angular 2 MockBackend\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#website\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"name\":\"Web Code Geeks\",\"description\":\"Web Developers Resource Center\",\"publisher\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.webcodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/www.webcodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/webcodegeeks\",\"https:\/\/x.com\/webcodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/b8e1e46688809b001b4e21bfe9e39312\",\"name\":\"Todd Leininger\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c876e1894ff2f10e3ff59df92895ce581b80b73b0c293ee0421c2f5e444e23f2?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c876e1894ff2f10e3ff59df92895ce581b80b73b0c293ee0421c2f5e444e23f2?s=96&d=mm&r=g\",\"caption\":\"Todd Leininger\"},\"description\":\"Todd Leininger is a Software Developer from the the Kansas City area. In his spare time he loves to play guitar and play video games. He is the proud to be raising two great children with his wife Michelle.\",\"url\":\"https:\/\/www.webcodegeeks.com\/author\/todd-leininger\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Setting Up Angular 2 MockBackend - Web Code Geeks - 2026","description":"I recently finished up on a client project developing one of the company\u2019s first Angular 2 applications, which would be the first such application to go","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/","og_locale":"en_US","og_type":"article","og_title":"Setting Up Angular 2 MockBackend - Web Code Geeks - 2026","og_description":"I recently finished up on a client project developing one of the company\u2019s first Angular 2 applications, which would be the first such application to go","og_url":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/","og_site_name":"Web Code Geeks","article_publisher":"https:\/\/www.facebook.com\/webcodegeeks","article_published_time":"2017-01-12T10:15:59+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg","type":"image\/jpeg"}],"author":"Todd Leininger","twitter_card":"summary_large_image","twitter_creator":"@webcodegeeks","twitter_site":"@webcodegeeks","twitter_misc":{"Written by":"Todd Leininger","Est. reading time":"11 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/#article","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/"},"author":{"name":"Todd Leininger","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/b8e1e46688809b001b4e21bfe9e39312"},"headline":"Setting Up Angular 2 MockBackend","datePublished":"2017-01-12T10:15:59+00:00","mainEntityOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/"},"wordCount":1407,"commentCount":0,"publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg","articleSection":["Angular.js"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/","url":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/","name":"Setting Up Angular 2 MockBackend - Web Code Geeks - 2026","isPartOf":{"@id":"https:\/\/www.webcodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/#primaryimage"},"image":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/#primaryimage"},"thumbnailUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg","datePublished":"2017-01-12T10:15:59+00:00","description":"I recently finished up on a client project developing one of the company\u2019s first Angular 2 applications, which would be the first such application to go","breadcrumb":{"@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/#primaryimage","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2014\/10\/angularjs-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/www.webcodegeeks.com\/javascript\/angular-js\/setting-angular-2-mockbackend\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.webcodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"JavaScript","item":"https:\/\/www.webcodegeeks.com\/category\/javascript\/"},{"@type":"ListItem","position":3,"name":"Angular.js","item":"https:\/\/www.webcodegeeks.com\/category\/javascript\/angular-js\/"},{"@type":"ListItem","position":4,"name":"Setting Up Angular 2 MockBackend"}]},{"@type":"WebSite","@id":"https:\/\/www.webcodegeeks.com\/#website","url":"https:\/\/www.webcodegeeks.com\/","name":"Web Code Geeks","description":"Web Developers Resource Center","publisher":{"@id":"https:\/\/www.webcodegeeks.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.webcodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.webcodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/www.webcodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/www.webcodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/webcodegeeks","https:\/\/x.com\/webcodegeeks"]},{"@type":"Person","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/b8e1e46688809b001b4e21bfe9e39312","name":"Todd Leininger","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.webcodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c876e1894ff2f10e3ff59df92895ce581b80b73b0c293ee0421c2f5e444e23f2?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c876e1894ff2f10e3ff59df92895ce581b80b73b0c293ee0421c2f5e444e23f2?s=96&d=mm&r=g","caption":"Todd Leininger"},"description":"Todd Leininger is a Software Developer from the the Kansas City area. In his spare time he loves to play guitar and play video games. He is the proud to be raising two great children with his wife Michelle.","url":"https:\/\/www.webcodegeeks.com\/author\/todd-leininger\/"}]}},"_links":{"self":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/15718","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/users\/211"}],"replies":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/comments?post=15718"}],"version-history":[{"count":0,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/posts\/15718\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media\/909"}],"wp:attachment":[{"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/media?parent=15718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/categories?post=15718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.webcodegeeks.com\/wp-json\/wp\/v2\/tags?post=15718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}