Enable Cross-Origin Resource Sharing (CORS)

Before you're able to use your server from your application, you will need to configure it to allow for Cross-Origin Resource Sharing (CORS) so that your Angular application hosted on http://localhost:5000 can communicate with your mock server hosted on http://localhost:3000:

  1. Install the cors package:
$ npm i cors
  1. Update index.js to use cors:
server/index.js...var cors = require('cors')...app.use(cors())// Initialize the Swagger middlewareswaggerTools.initializeMiddleware(swaggerDoc, function(middleware) {...
Ensure that app.use(cors()) is called right before initializeMiddleware; otherwise, other Express middleware may interfere with the functionality of cors().

Get Angular 6 for Enterprise-Ready Web Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.