我使用Node作为我的服务器,使用angular作为我的前端服务。 我从npm安装了cors。即使使用CORS标题后仍然出现相同的错误。是因为我的函数没有限制我的app.get()。
如何在我的案例中实施?
// ## =======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 !!!');
});

如果我在chrome中使用cors插件,该功能可以正常工作,但我想以适当的方式找到解决方案。我刚才也讨论过这个问题Node.js CORS error
答案 0 :(得分:2)
浏览器首先发送HTTP OPTIONS
预检请求,而SOAP服务器显然没有很好地处理它,因为它返回错误响应。
如果strong-soap没有对CORS的任何支持,你可以尝试在SOAP服务器前放置一个nginx代理,它将响应CORS预检请求。
答案 1 :(得分:0)
要做:
npm install cors --save
添加:
const cors = require('cors')
app.use(cors())