In summary I am using a viewer like api of dicom files called cornerstone, for this I connect to the WADO service of dc4chee to get the dicom, dcm4chee runs port 8080, and my application on node uses port 3000, so I am trying to show The browser's dicom.
总而言之,我正在使用像dicom文件的api这样的观众,称为基石,为此我连接到dc4chee的WADO服务以获取dicom,dcm4chee运行端口8080,我的节点上的应用程序使用端口3000,所以我试图显示浏览器的dicom。
https://www.npmjs.com/package/cornerstone-wado-image-loader
This is the error displayed by the browser
这是浏览器显示的错误
XMLHttpRequest can not load http: // localhost: 8080 / wado? RequestType = WADO & studyUID = 1.2.840.113704.1.111.5 ... 26513.429 & contentType = application% 2Fdicom & transferSyntax = 1.2.840.10008.1.2. In 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http: // localhost: 3000' is therefore not allowed access.
In the documentation specified
在指定的文档中
Note that the web server must support Cross source resource sharing or the image will fail to load. If you are unable to get CORS enabled on the web server you are loading DICOM P10 instances from, you can use a reverse proxy. Here's a simple Node.js based on http-proxy that adds CORS headers that you might find useful.
请注意,Web服务器必须支持跨源资源共享,否则图像将无法加载。如果您无法在Web服务器上启用CORS,而您正在加载DICOM P10实例,则可以使用反向代理。这是一个基于http-proxy的简单Node.js,它添加了你可能觉得有用的CORS头文件。
And show this example code but I'm using express and this code does not work
并显示此示例代码,但我使用快递,此代码不起作用
Var http = require ('http'),
HttpProxy = require ('http-proxy');
Var proxy = httpProxy.createProxyServer ({target: 'http: // localhost: 8042'}) .listen (8000);
Proxy.on ('proxyRes', function (proxyReq, req, res, options) {
// add the CORS header to the response
Res.setHeader ('Access-Control-Allow-Origin', '*');
});
Proxy.on ('error', function (e) {
// suppress errors
});
Also use npm cors here the code
也可以在这里使用npm cors代码
Var express = require ('express')
Var cors = require ('cors')
Var app = express ()
App.get ('/ products /: id', cors (), function (req, res, next) {
Res.json ({msg: 'This is CORS-enabled for a Single Route'))
})
App.listen (80, function () {
Console.log ('CORS-enabled web server listening on port 80')
})
But with this I enable the cors on port 3000 and not the 8080, I need the mode to activate or add 'Access-Control-Allow-Origin in headers response and not in header request,
但是有了这个我在端口3000而不是8080上启用了cors,我需要模式来激活或在头响应中添加'Access-Control-Allow-Origin而不是在头请求中,
How can I do to add CORS on port 8080 where dcm4chee runs from NODEjs?
如何在端口8080上添加CORS,其中dcm4chee从NODEjs运行?
sorry for my English
对不起我的英语不好
update!
The server responds with the following;
服务器响应以下内容;
RESPONDE HEADER
Content-Type:application/dicom
Date:Sat, 01 Apr 2017 01:15:38 GMT
Expires:0
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked
X-Powered-By:Servlet 2.4; JBoss-4.2.3.GA (build: SVNTag=JBoss_4_2_3_GA
date=200807181439)/JBossWeb-2.0
REQUEST HEADER
Accept:*/*
Accept-Encoding:gzip, deflate, sdch, br
Accept-Language:es-ES,es;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Host:localhost:8080
Origin:http: //localhost:3000
Referer:http: //localhost:3000/
User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like
Gecko) Chrome/55.0.2883.87 Safari/537.36
HOW TO ENABLE THE CORS IN RESPONSE HEADER??
如何在响应头上启用CORS?
4 个解决方案
#1
6
do
npm install cors --save
and just add these lines in your main file where your request is going.
并在您的请求所在的主文件中添加这些行。
const cors = require('cors');
const express = require('express');
let app = express();
app.use(cors());
app.options('*', cors());
#2
2
To enable cors you can do this:
要启用cors,您可以执行以下操作:
var cors = require('cors');
app.use(cors());
// to change your ports for different cors stuff:
app.set('port', process.env.PORT || 3000);
app.listen(app.get('port'), function() {
console.log('we are listening on: ',
app.get('port'))
});
Remember that cors are middleware, so you will want to have app.use
before it so that your incoming requests will go through cors before they hit your routes.
请记住,cors是中间件,因此您需要在它之前安装app.use,以便您的传入请求在它们到达您的路线之前通过cors。
You can change the ports depending on which one you want to use. I am pretty sure you can also replace the ||
with &&
to listen on multiple ports and set cors on those.
您可以根据要使用的端口更改端口。我很确定你也可以替换||用&&监听多个端口并在那些端口上设置cors。
In raw node, I believe you have to use the writeHead, but I am not sure about the raw node implementation.
在原始节点中,我相信你必须使用writeHead,但我不确定原始节点的实现。
#3
1
The error displayed by the browser means, server localhost:8080 refused a request from localhost:3000, It seems cors didn't set well on server localhost:8080.
浏览器显示错误,服务器localhost:8080拒绝来自localhost:3000的请求,似乎cors在服务器localhost:8080上设置不好。
The response header should have something like this:
响应头应该是这样的:
Access-Control-Allow-Headers:Content-Type,Content-Length, Authorization, Accept,X-Requested-With
Access-Control-Allow-Methods:PUT,POST,GET,DELETE,OPTIONS
Access-Control-Allow-Origin:*
Try add cors header in your 8080 server.
尝试在8080服务器中添加cors标头。
app.all('*', function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
//...
#4
0
Adding CORS(Cross-Origin-Resource-Sharing) to your node, express app is quite easy...
将CORS(跨源资源共享)添加到您的节点,表达应用程序非常简单......
You need to install cors library via npm first, using the command below:
您需要先使用以下命令通过npm安装cors库:
npm install cors -S
and if you need it globally, just add -g
flag to it...
如果你需要全局,只需添加-g标志...
Then in your express app, do this:
然后在您的快递应用中,执行以下操作:
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
Also these are other examples for cors from their doc:
这些是他们的文档中的其他cors示例:
var express = require('express')
var cors = require('cors')
var app = express()
app.use(cors())
app.get('/products/:id', function (req, res, next) {
res.json({msg: 'This is CORS-enabled for all origins!'})
})
app.listen(80, function () {
console.log('CORS-enabled web server listening on port 80')
})
Configuring CORS Asynchronously:
异步配置CORS:
var express = require('express')
var cors = require('cors')
var app = express()
var whitelist = ['http://example1.com', 'http://example2.com']
var corsOptionsDelegate = function (req, callback) {
var corsOptions;
if (whitelist.indexOf(req.header('Origin')) !== -1) {
corsOptions = { origin: true } // reflect (enable) the requested origin in the CORS response
}else{
corsOptions = { origin: false } // disable CORS for this request
}
callback(null, corsOptions) // callback expects two parameters: error and options
}
app.get('/products/:id', cors(corsOptionsDelegate), function (req, res, next) {
res.json({msg: 'This is CORS-enabled for a whitelisted domain.'})
})
app.listen(80, function () {
console.log('CORS-enabled web server listening on port 80')
})
#1
6
do
npm install cors --save
and just add these lines in your main file where your request is going.
并在您的请求所在的主文件中添加这些行。
const cors = require('cors');
const express = require('express');
let app = express();
app.use(cors());
app.options('*', cors());
#2
2
To enable cors you can do this:
要启用cors,您可以执行以下操作:
var cors = require('cors');
app.use(cors());
// to change your ports for different cors stuff:
app.set('port', process.env.PORT || 3000);
app.listen(app.get('port'), function() {
console.log('we are listening on: ',
app.get('port'))
});
Remember that cors are middleware, so you will want to have app.use
before it so that your incoming requests will go through cors before they hit your routes.
请记住,cors是中间件,因此您需要在它之前安装app.use,以便您的传入请求在它们到达您的路线之前通过cors。
You can change the ports depending on which one you want to use. I am pretty sure you can also replace the ||
with &&
to listen on multiple ports and set cors on those.
您可以根据要使用的端口更改端口。我很确定你也可以替换||用&&监听多个端口并在那些端口上设置cors。
In raw node, I believe you have to use the writeHead, but I am not sure about the raw node implementation.
在原始节点中,我相信你必须使用writeHead,但我不确定原始节点的实现。
#3
1
The error displayed by the browser means, server localhost:8080 refused a request from localhost:3000, It seems cors didn't set well on server localhost:8080.
浏览器显示错误,服务器localhost:8080拒绝来自localhost:3000的请求,似乎cors在服务器localhost:8080上设置不好。
The response header should have something like this:
响应头应该是这样的:
Access-Control-Allow-Headers:Content-Type,Content-Length, Authorization, Accept,X-Requested-With
Access-Control-Allow-Methods:PUT,POST,GET,DELETE,OPTIONS
Access-Control-Allow-Origin:*
Try add cors header in your 8080 server.
尝试在8080服务器中添加cors标头。
app.all('*', function (req, res) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Content-Type,Content-Length, Authorization, Accept,X-Requested-With");
res.header("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
//...
#4
0
Adding CORS(Cross-Origin-Resource-Sharing) to your node, express app is quite easy...
将CORS(跨源资源共享)添加到您的节点,表达应用程序非常简单......
You need to install cors library via npm first, using the command below:
您需要先使用以下命令通过npm安装cors库:
npm install cors -S
and if you need it globally, just add -g
flag to it...
如果你需要全局,只需添加-g标志...
Then in your express app, do this:
然后在您的快递应用中,执行以下操作:
const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());
Also these are other examples for cors from their doc:
这些是他们的文档中的其他cors示例:
var express = require('express')
var cors = require('cors')
var app = express()
app.use(cors())
app.get('/products/:id', function (req, res, next) {
res.json({msg: 'This is CORS-enabled for all origins!'})
})
app.listen(80, function () {
console.log('CORS-enabled web server listening on port 80')
})
Configuring CORS Asynchronously:
异步配置CORS:
var express = require('express')
var cors = require('cors')
var app = express()
var whitelist = ['http://example1.com', 'http://example2.com']
var corsOptionsDelegate = function (req, callback) {
var corsOptions;
if (whitelist.indexOf(req.header('Origin')) !== -1) {
corsOptions = { origin: true } // reflect (enable) the requested origin in the CORS response
}else{
corsOptions = { origin: false } // disable CORS for this request
}
callback(null, corsOptions) // callback expects two parameters: error and options
}
app.get('/products/:id', cors(corsOptionsDelegate), function (req, res, next) {
res.json({msg: 'This is CORS-enabled for a whitelisted domain.'})
})
app.listen(80, function () {
console.log('CORS-enabled web server listening on port 80')
})