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