Node.JenCourse错误

问题描述 投票:1回答:1

我使用Node作为后端服务器,Angular 4作为前端。我试图通过soap协议发送查询结果。该功能正常,但在浏览器中安装了CORS PLUGIN。但是如何在没有这些插件的情况下解决它。

我已经搜索了一些解决方案,我已经在我的节点文件中安装了CORS包,并且还对我的server.js文件执行了以下操作。

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

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

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, PUT, DELETE');
  res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
  res.setHeader('Access-Control-Allow-Credentials', true);
  next();
});

但它没有帮助。错误仍然存​​在。如何解决此错误。我已发布我的服务器端代码如下:

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

function dbQuery() {
  var response = 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',
  });
  console.log("response is " + response);
  return response;

}

function main2() {
  var result2 = dbQuery();

  return result2.then(function(test) {

    console.log("test is " + JSON.stringify(test._result));
    var IP = test._result.filter(Boolean);
    console.log("Ip is " + IP);

    var xml = require('fs').readFileSync('check_username.wsdl', 'utf8');
    var soap_msg = '<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:examples:CheckUserNameService"$
    '<soapenv:Header/>' + '<soapenv:Body>' + '<urn:CheckUserNameResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">' + '<status xsi:type="xsd:string">' + JSON.stringify(test._result) + '</status>' + '</urn:CheckUserNameResponse>' +
      '</soapenv:Envelope>';

    var server = http.createServer(function(request, response) {
      response.end(soap_msg);
    });

    var port = 8000;
    server.listen(port);
    var soapServer = soap.listen(server, '/test', xml);
    console.log('SOAP service listening on port ' + port);
  });
}
main2();

错误

enter image description here

node.js cors
1个回答
3
投票

您可以将选项传递给core函数,如下所示:

const corsOpts = {
  origin: '*',

  methods: [
    'GET',
    'POST',
  ],

  allowedHeaders: [
    'Content-Type',
  ],
};

app.use(cors(corsOpts));
© www.soinside.com 2019 - 2024. All rights reserved.