Code Monkey home page Code Monkey logo

fake-backend-interceptor's Introduction

fake-backend-interceptor

Set up a HttpCient interceptor for backend-less development in Angular 4.3 +

Bellow you can find a very simple way to set up a backend mock that allows you to implement your angular app without having the server side running.

I've built the backend mock as a HTTP_INTERCEPTOR, so every HTTP request will pass trought.

import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { HttpInterceptor } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { HttpRequest } from '@angular/common/http';
import { HttpHandler } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { HttpEvent } from '@angular/common/http';


@Injectable()
export class FakeBackendInterceptor implements HttpInterceptor {
   constructor() { }

   intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
      if (request.url.endsWith('/users') && request.method === 'GET' && request.params.get('mock')) {
         console.log('HTTP REQUEST INTERCEPTED BY THE MOCKED BACKEND');
         request = request.clone({
            url: '/assets/json/users.json'
         });
      }

      return next.handle(request);
   }
}


export let fakeBackendProvider = {
   // use fake backend in place of Http service for backend-less development
   provide: HTTP_INTERCEPTORS,
   useClass: FakeBackendInterceptor,
   multi: true
};

Then, we need to put the response content available somewhere in the app. In this example, I've put it in the assets direcory. The content of the json file could be something like that:

[
    {
        id: 1
        name: 'John',
        surname: 'Rambo',
        birthdate: '1946/06/06'
    },
    {
        id: 2
        name: 'Sam',
        surname: 'Trautman',
        birthdate: '1929/07/04'
    }
]

Then, add the provider to your app.module.ts and thats all.

fake-backend-interceptor's People

Contributors

glucena avatar

Watchers

James Cloos avatar ackuser avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.