如何使用Express启用Cors Node.js? [英] How to enable cors nodejs with express?

查看:194
本文介绍了如何使用Express启用Cors Node.js?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

总而言之,我使用的是类似dicom文件的api的查看器,称为Cornerstone,为此,我连接到dc4chee的WADO服务以获取dicom,dcm4chee运行端口8080,而节点上的应用程序使用端口3000,试图显示浏览器的dicom.

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.

https://www.npmjs.com/package/cornerstone-wado-图像加载器

这是浏览器显示的错误

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服务器必须支持跨源资源共享,否则图像将无法加载.如果无法从要加载DICOM P10实例的Web服务器上启用CORS,则可以使用反向代理.这是一个基于http-proxy的简单Node.js,它添加​​了您可能会觉得有用的CORS标头.

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.

并显示此示例代码,但我使用的是Express,此代码不起作用

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
});

也请在此处使用npm cors

Also use npm cors here the code

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'.

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,

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

How can I do to add CORS on port 8080 where dcm4chee runs from NODEjs?

对不起,我的英语

更新!

服务器响应以下内容;

The server responds with the following;

响应头

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?

HOW TO ENABLE THE CORS IN RESPONSE HEADER??

推荐答案

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());

这篇关于如何使用Express启用Cors Node.js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆