Radek S Radek S - 1 year ago 1633
AngularJS Question

Setting Access-Control-Allow-Origin header in Angular2 development mode

I am making HTTP requests to an external server which of CORS don't make it through.

In production I will set the Access-Control-Allow-Origin header in nginx.
Is there a way to also set the header in the "built-in" Angular's development server so that I don't need to run any proxy on my laptop?

Answer Source

I think that you need to provide a custom configuration to register the cors module as middleware:

'use strict';
var fallback = require('connect-history-api-fallback');
var log = require('connect-logger');
var cors = require('cors');

module.exports = {
  injectChanges: false, // workaround for Angular 2 styleUrls loading
  files: ['./**/*.{html,htm,css,js}'],
  watchOptions: {
    ignored: 'node_modules'
  server: {
    baseDir: './',
    middleware: [
      log({format: '%date %status %method %url'}),
        index: '/index.html',
        htmlAcceptHeaders: ['text/html', 'application/xhtml+xml']

To use the custom configuration file use the c or config parameter:

"lite": "lite-server -c custom-config.js",

Here is the cors module:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download