24 April 2020

How to create dummy REST service in Angular 2 for testing.

install json-server package using
$npm install -g json-server


Now create fake REST API as below
create db.json file
{
  "posts": [
    { "id": 1, "title": "json-server", "author": "typicode" }
  ],
  "comments": [
    { "id": 1, "body": "some comment", "postId": 1 }
  ],
  "profile": { "name": "typicode" }
}
run below command from the same directory where db.json is saved.
$ json-server --watch db.json

Now if you go to http://localhost:3000/posts/1, you'll get
{ "id": 1, "title": "json-server", "author": "typicode" }

references:
https://www.npmjs.com/package/json-server
alternative way explained in https://www.npmjs.com/package/angular2-in-memory-web-api / https://www.concretepage.com/angular-2/angular-2-http-post-example


Angular : The Big Picture (PluralSight Training)

command to create new project
ng new DRAFTRTI

command to create new component
navigate to directory of project like cd DRAFTRTI
ng g c draft

to run the application run
ng serve

No comments:

Post a Comment