Npm start - using CORS

I am working on a small application using Angular2. At 5min. The npm quick start guide is used to install the necessary modules and launch the Lite server. Later I want to use this application in a regular web server and create mobile applications with a corridor. Be that as it may, I use the REST api to load some data. How to configure a Lite server to verify validation before CORS flights? My Apache server (on a dedicated server) is already configured, but I donโ€™t want to commit, update and recompile every small change. I need the appropriate configuration:

Header always set Access-Control-Allow-Origin "https://my-domain.net:12345" Header always set Access-Control-Allow-Headers "X-Requested-With, Content-Type, Origin, Authorization, Accept, Client-Security-Token, Accept-Encoding" Header always set Access-Control-Allow-Methods "POST, GET, OPTIONS, DELETE, PUT" Header always set Access-Control-Max-Age "1000" 
+6
source share
3 answers

There is no built-in option inside the Lite server. However, you can fix it, but I really donโ€™t see what you are trying to do here ...

After installing it, you can enter the node_modules/lite-server folder and install the connect-cors :

 npm install connect-cors 

Then in lib/lite-server.js file you can import the module and use its aftermarket array after:

 var historyFallback = require('connect-history-api-fallback'); var log = require('connect-logger'); var cors = require('connect-cors'); // <------------------ (...) sync.init({ port: argv.port || 3000, server: { baseDir: options.baseDir, middleware: [ cors(), // <------------------ log(), historyFallback({ index: options.fallback }) ] }, files: options.files, }); 

When starting the server with the following command, CORS support is supported:

 ./bin/lite-server --baseDir ../../ 

By adding the Origin header to your requests, you will see the CORS headers in the response.

+5
source

in fact, the Lite server is just a browser synchronization shell, if you can check documents from https://www.browsersync.io/docs/options , you can find the cors configuration, and since lite server support uses the configuration file to parameter settings, so all you have to do is just install the configuration file for the Lite server, you can choose bs-config.js or bs-config.json if we use the simplest bs-config.json, contente:

 { "cors": true } 

then you immediately launch the Lite server, you will see:

Sync browser configuration

 { injectChanges: false, files: [ './**/*.{html,htm,css,js}' ], watchOptions: { ignored: 'node_modules' }, server: { baseDir: './', middleware: [ [Function], [Function] ] }, cors: true } 

[BS] Access URLs:

  Local: http://localhost:3000 

which means cors is working.

+2
source

I found with http-server

http-server -o --cors

+1
source

All Articles