【发布时间】:2017-08-26 06:41:08
【问题描述】:
总而言之,我正在使用一个名为基石的 dicom 文件 api 之类的查看器,为此我连接到 dc4chee 的 WADO 服务以获取 dicom,dcm4chee 运行端口 8080,而我在节点上的应用程序使用端口 3000,所以我试图显示浏览器的 dicom。
https://www.npmjs.com/package/cornerstone-wado-image-loader
这是浏览器显示的错误
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.
在指定的文档中
请注意,网络服务器必须支持跨源资源共享,否则图片将无法加载。如果您无法在要从中加载 DICOM P10 实例的 Web 服务器上启用 CORS,则可以使用反向代理。这是一个基于 http-proxy 的简单 Node.js,它添加了您可能会发现有用的 CORS 标头。
并显示此示例代码,但我使用的是 express 并且此代码不起作用
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
});
这里的代码也使用 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')
})
但是有了这个,我在端口 3000 而不是 8080 上启用了 cors,我需要在标头响应中而不是在标头请求中激活或添加“Access-Control-Allow-Origin”的模式,
如何在 dcm4chee 从 NODEjs 运行的端口 8080 上添加 CORS?
更新!
服务器响应如下;
响应标题
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
请求标头
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
如何在响应头中启用 CORS??
【问题讨论】:
-
这段代码真的启动了服务器吗?您有几个语法错误...
Var不能大写,并且您正在大写变量名(区分大小写),就像使用App但您将其定义为app。 -
How to allow CORS?的可能重复
-
看
App.get ('/ products /: id', cors (), function (req, res, next) { Res.json ({msg: 'This is CORS-enabled for a Single Route')) })-Res和res不是同一个变量
标签: javascript node.js express cors