How to enable cors nodejs with expression?

In conclusion, I use the dicom api file viewer called the cornerstone, for this I connect to the WADO dc4chee service to get dicom, dcm4chee starts port 8080, and my application on node uses port 3000, so I'm trying to show the dicom browser.

https://www.npmjs.com/package/cornerstone-wado-image-loader

This is a browser error.

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 specified documentation

Please note that the web server must support Cross Source sharing or the image does not load. If you cannot enable CORS on the web server, you download DICOM P10 instances, you can use a reverse proxy. Here is a simple http-proxy-based Node.js that adds CORS headers that you might find useful.

And show this sample code, but I use 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 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')
})

But with this I enable cors on port 3000, not 8080, I need a mode to activate or add "Access-Control-Allow-Origin" in the response of the headers, and not in the request for the header,

How to do to add CORS to port 8080, where dcm4chee starts from NODEjs?

sorry for my English

update!

The server responds as follows:

REPOND HEAD

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

TITLE RECORDING

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 INCLUDE COURSES REGARDING DEPENDENCE?

+4
source share
4 answers

do

npm install cors --save

and just add these lines to your main file where your request will be executed.

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

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

, cors , app.use , cors, .

, . , || &&, .

raw node, , writeHead, raw node.

+2

, , , localhost: 8080 localhost: 3000. , cors : 8080.

- :

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:*

cors 8080.

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");
//...
0

CORS (Cross-Origin-Resource-Sharing) node, - ...

cors npm, :

npm install cors -S

, -g...

- :

const express = require('express');
const cors = require('cors');
const app = express();
app.use(cors());

Also these are other examples for cors from their document:

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:

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')
})
0
source

Source: https://habr.com/ru/post/1673747/


All Articles