0

I am using Node as my server and angular as my front end service. I have installed cors from npm. Even after using the CORS headers still I am getting the same error. Is it because my function is not bounded my app.get().

How can I implement in my case ?

// ## =======BASE SETUP======= ##

const arangojs = require('arangojs');
const express = require('express');
const aqlQuery = arangojs.aqlQuery;
const bodyParser = require('body-parser');

// ## Const variables for connecting to ArangoDB database

const dbConfig = {
  host: '0.0.0.0',
  port: '8529',
  username: 'xyz',
  password: 'xxyz',
  database: 'sgcdm2',
};

// ## Connection to ArangoDB

const db = new arangojs.Database({
  url: `http://${dbConfig.host}:${dbConfig.port}`,
  databaseName: dbConfig.database
});

db.useBasicAuth(dbConfig.username, dbConfig.password);

var soap = require('strong-soap').soap;
var http = require('http');
var fs = require('fs');

//CORS PLUGIN
var cors = require('cors');
var app = express();
app.use(cors());
app.use(function(req, res, next) {
  res.setHeader('Access-Control-Allow-Origin', '*');
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
  res.setHeader('Access-Control-Allow-Credentials', true);
  next();
});

var test = {};
test.server = null;
test.service = {
    CheckUserName_Service: {
      CheckUserName_Port: {
        //first Query function
        checkUserName: function(args, callback, soapHeader, req, res) {

            //CORS PLUGIN

            res.setHeader('Access-Control-Allow-Origin', '*');
            res.setHeader('Access-Control-Allow-Methods', 'GET, POST');
            res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
            res.setHeader('Access-Control-Allow-Credentials', true);
            console.log('checkUserName: Entering function..');

            db.query(aqlQuery `
                                LET startVertex = (FOR doc IN spec
                                FILTER doc.serial_no == '"123456abcde"'
                                LIMIT 2
                                RETURN doc
                                )[0]

                                FOR v IN 1 ANY startVertex belongs_to
                                RETURN v.ip`, {
              bindVar1: 'value',
              bindVar2: 'value',
            }).then(function(response) {
              console.log("response is " + JSON.stringify(response._result));
              callback(({
                status: JSON.stringify(response._result)
              }));

            });
            var wsdl = require('fs').readFileSync('check_username.wsdl', 'utf8');
            fs.readFile('./check_username.wsdl', 'utf8', function(err, data) {
              test.wsdl = data;
              test.server = http.createServer(function(req, res) {
                res.statusCode = 404;
                res.end();
              });

                test.server.listen(8000, null, null, function() {
                test.soapServer = soap.listen(test.server, '/test/server2.js', test.service, test.wsdl);
                test.baseUrl = 'http://' + test.server.address().address + ":" +               test.server.address().port;
              });
              console.log('server listening !!!');

            });

If I use cors plugin in chrome, the function works fine without any trouble but I would like to find a solution in a proper way. I have also discussed this problem a while ago Node.js CORS error

enter image description here

cantona_7
  • 1,127
  • 4
  • 21
  • 40
  • See https://stackoverflow.com/questions/48133339/make-cors-request-with-polymer-iron-ajax-and-node-js/48133489#48133489. You need too add `app.options('*', cors())` to your node code, in order to handle the CORS preflight OPTIONS request that the browser sends. – sideshowbarker Apr 13 '18 at 23:53
  • @sideshowbarker I have added this..,but still the error persists !! is this beacuse of soap ? – cantona_7 Apr 16 '18 at 08:01

2 Answers2

2

The browser sends the HTTP OPTIONS preflight request first and the SOAP server doesn't handle it well obviously because it returns an error response.

If the strong-soap doesn't have any support for CORS you could try putting an nginx proxy in front of the SOAP server which would response the CORS preflight requests.

Jan Steuer
  • 141
  • 4
  • location /socket/ { proxy_pass http://127.0.0.1:3300; } ... something like this ?? – cantona_7 Apr 13 '18 at 11:30
  • Sorry I never did this before..,I just have add the file to my node file or should I reconfigure it ? – cantona_7 Apr 16 '18 at 08:03
  • 1
    This is a config file for Nginx, which is a standalone server and it won't be part of your app. It just runs somewhere and redirects all the requests to your Express app. – Jan Steuer Apr 16 '18 at 09:01
  • 1
    It's worth spending some time learning Nginx as you'll surely hit it sooner or later. Eg. most node.js apps don't serve static resources like images by itself but let nginx do it as it can do it faster. https://www.nginx.com/ – Jan Steuer Apr 16 '18 at 09:07
0

Do:

npm install cors --save

Add:

const cors = require('cors')
app.use(cors())
4b0
  • 21,981
  • 30
  • 95
  • 142