본문 바로가기

개발/Web

Http 통신

  • Angular 애플리케이션은 HTTP 프로콜을 통하여 서버와 통신함

REST API Mock 서버 구축(json-server)

  1. json-server 설치

     npm install -g json-server
  2. db.json 작성

     [
       {
           "id": 1,
           "name": "hello",
           "completed": false
       },
       {
           "id": 2,
           "name": "world",
           "completed": false
       },
       {
           "id": 3,
           "name": "lisa",
           "completed": false
       }
     ]
  3. 프로젝트 생성

     ng new 'projectName'
  4. app.module.ts에 HttpClient 추가(HttpClient를 애플리케이션 전역에서 사용할 수 있도록)

     //@angular/common/http  패키지의 HttpClientModule을 임포트
     import { HttpClientModule } from '@angular/common/http';
  5. 사용

     import {Injectable } from '@angular/core';
     import { HttpClient } from '@angular/common/http';
    
     @Injectable()
     export class SomeService{
         constructor(private http: HttpClient){}
     }
    
     //HTTP Get 요청
     this.http.get('api/todos').subscribe(...);

'개발 > Web' 카테고리의 다른 글

Angular UI 라이브러리  (0) 2020.04.13
Rest(Representational State Transfer) API  (0) 2020.04.03
Angular의 구성요소  (0) 2020.04.02
Angular 파일 구조 및 흐름  (0) 2020.04.02
Router(라우터)  (0) 2020.03.31