nodeS CORS的问题

时间:2017-03-31 01:47:59

标签: node.js cors dicom

我正在尝试使用NODEJS显示一个dicom,但是浏览器会抛出这个问题

  

XMLHttpRequest无法加载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。在'Access-Control-Allow-Origin'标头中存在于请求的资源上。 Origin'http:// localhost:3000'是   因此不允许访问。

然后安装npm the package cors

在这里,我将代码作为代码app.js

的一部分
Var express = require ('express');
Var cors = require ('cors')
App.use (cors ())


App.listen (3000, function () {
  Console.log ('listening on 3000')
})

另请尝试在app.js中添加此内容

App.use (function (req, res, next) {
  Res.header ("Access-Control-Allow-Origin", "*");
  Res.header ("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  Next ();
});

但浏览器仍显示错误:

  

XMLHttpRequest无法加载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。在'Access-Control-Allow-Origin'标头中存在于请求的资源上。 Origin'http:// localhost:3000'是   因此不允许访问。

我有疑问,因为我也使用我留在这里的路线index.js

var express = require('express');
var router = express.Router();
var cors = require('cors')
/*var express = require('express');
var router = express.Router();*/
var mysql = require('mysql');
router.use(cors({origin: 'http://localhost:3000'}));
/* GET home page. */
router.get('/', function(req, res, next) {
 res.render('index', { title: 'Express' });
});

module.exports = router;

对不起我的英文

1 个答案:

答案 0 :(得分:0)

出于安全考虑,浏览器在正常情况下不允许跨源请求。

您的页面是从localhost:3000加载的,它定义了作为第一个请求的来源。您的代码正在尝试从端口-8080上运行的另一个服务加载数据,这与源不同。所以基本上你需要在端口8080上运行的服务上启用cors,因为它是外部源。