2

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?

1 Answer 1

1

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'}),
      cors(),
      fallback({
        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:

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Thierry! (In the meantime I installed a browser plugin to modify the response headers, also an alternative).
When editing the configuration file (in node_modules/lite-server/lib/config-defaults.js), I still have the same error: 'Failed to load resource: Origin localhost:3000 is not allowed by Access-Control-Allow-Origin'. An idea to resolve it?
@RadekSkokan which browser plugin did you install?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.