npm start-使用CORS [英] npm start - using CORS
问题描述
我正在使用Angular2开发一个小应用程序.在 5分钟内.快速入门指南 npm用于安装必需的模块和启动lite服务器.稍后,我想在普通的网络服务器中使用该应用程序,并使用cordova构建移动应用程序.但是,我正在使用REST API加载一些数据.如何配置lite服务器以解决CORS飞行前选项检查?我的apache服务器(在专用服务器上)已经配置好了,但是我不想提交,更新和重新编译每一个小的更改.我需要与此对应的配置:
I'm working on a little app, using Angular2. In the 5min. Quickstart Guide npm is used to install required modules and to start a lite-server. Later I want to use that app within a normal web-server and build mobile apps with cordova. How ever, I'm using a REST api to load some data. How can I configure the lite-server to solve CORS pre-flight OPTIONS check? My apache server (on a dedicated server) is already configured, but I don't want to commit, update and recompile every little change. I need a corresponding configuration to this:
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"
推荐答案
lite服务器中没有内置选项可以做到这一点.就是说,您可以修补它,但我真的看不到您在这里尝试做什么...
There is no built-in option to do that within the lite-server. That said, you can patch it but I don't really see what you try to do here...
安装后,您可以进入node_modules/lite-server
文件夹并安装
After having installed it, you can go into the node_modules/lite-server
folder and install the connect-cors
module:
npm install connect-cors
然后在lib/lite-server.js file
中,您可以导入模块,并在之后使用它作为中间件数组:
Then in the lib/lite-server.js file
, you can import the module and use it the middleware 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,
});
使用以下命令启动服务器时,可以使用CORS支持:
When starting the server with the following command the CORS support is usable:
./bin/lite-server --baseDir ../../
通过在请求中添加Origin
标头,您将在响应中看到CORS标头.
By adding an Origin
header in requests, you will see CORS headers in the response.
这篇关于npm start-使用CORS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!