This code demo uses the Mock Service Worker (msw) library to return mocked HTTP responses.
- Go to the
/code-demos/mock-service-workerfolder. - Run
npm ito install all the packages. - Run
npm startto start the app which will be available on http://localhost:4202.
This solution uses the msw npm package which following the Getting started guide and the Browser integration guide. The implementation details are:
- install
mswnpm package. - run the
npx msw init src/mock-api-responsescommand. This will add themockService.Worker.jsscript in thesrc/mock-api-responsesfolder, which is where I decided to put everything related withmsw. - instruct Angular to serve the
mockService.Worker.jsscript from the root, meaning at http://localhost:4202/mockService.Worker.js. To do this an extra entry was added to the assets configuration in the angular.json file:
{
"glob": "mockServiceWorker.js",
"input": "src/mock-api-responses",
"output": "./"
}- create the browser.ts and handlers.ts files as per
mswdocumentation. - run the
mswworker at app startup if the app is in development mode. This is done in the app.module.ts.