微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

javascript – 如何使用express启用cors nodejs?

总而言之,我正在使用像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.

在指定的文档中

请注意,Web服务器必须支持跨源资源共享,否则图像将无法加载.如果您无法在Web服务器上启用CORS,而您正在加载DICOM P10实例,则可以使用反向代理.这是一个基于http-proxy的简单Node.js,它添加​​了你可能觉得有用的CORS头文件.

显示此示例代码,但我使用快递,此代码不起作用

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而不是在头请求中,

如何在端口8080上添加CORS,其中dcm4chee从NODEjs运行?

对不起我的英语不好

更新!

服务器响应以下内容;

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

请求头

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?

解决方法:

npm install cors --save

并在您的请求所在的主文件添加这些行.

const cors = require('cors');
const express = require('express');
let app = express();
app.use(cors());
app.options('*', cors());

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐