gpt4 book ai didi

node.js - 带有 Express.js 的 SOAP 服务器

转载 作者:搜寻专家 更新时间:2023-10-31 23:35:11 27 4
gpt4 key购买 nike

我使用 node-soap 创建了一个简单的 SOAP 服务器当前正在为来自 SOAP 客户端的请求提供服务。现在的要求是为将来来自不同客户端的传入 REST 和 SOAP 请求提供服务。我的问题是我可以通过使用单个 Express 应用程序(使用 Express.js 框架)来实现吗?我是新来的,所以任何帮助将不胜感激。提前致谢!

最佳答案

我认为prasun已经给出了很好的例子,但想更详细地分享我的例子。我用了expresssoap , 但使用 http/https 创建服务器而不是使用 express

首先,下面是项目结构,我模块化了 RESTful 端点路由器的路由文件夹和 Soap WebService 端点的服务文件夹

/config/index.js    
/routes/index.js
/services/wsdl/poservice.wsdl
/services/poservice.js
app.js
server.js

通过 express 创建路由器并为根 ('/') 上下文路径的 GET 请求构建端点

[路由器/index.js]

var express = require('express');
var router = express.Router();

router.get('/', function(req, res, next) {
res.setHeader('Content-Type', 'application/json');
res.send(JSON.stringify({"data": "some data"}));
});

module.exports = router;

在 app.js 上构建主路由映射

[app.js]

var express = require('express');
var bodyParser = require('body-parser');
var indexRouter = require('./routes/index');

var app = express();
app.use(bodyParser.raw({type: function(){return true;}, limit: '5mb'}));

app.use('/', indexRouter);
module.exports = app;

设置服务、端口和操作对象,并从文件路径发布 wsdl (./services/wsdl/PurchaseOrderServiceV2.wsdl)

[服务/poservice.js]

var fs = require('fs');
var path = require('path');
var poServiceXml = fs.readFileSync(path.join(path.join(__dirname, 'wsdl'), 'PurchaseOrderServiceV2.wsdl'), 'utf8');

var poService = {
PurchaseOrderService: {
PurchaseOrderServiceSoapHttpPort: {
getPurchaseOrder: function(args) {
console.log(args);
return {
name: args.name
};
}
}
}
};

module.exports = {
service: poService,
xml: poServiceXml
};

此服务、端口和操作设置基于 wsdl。下面是截取的 wsdl 服务、端口和操作定义

[服务/wsdl/PurchaseOrderServiceV2.wsdl]

<wsdl:operation name="getPurchaseOrder">
<wsdl:input message="tns:PurchaseOrderService_getPurchaseOrder"/>
<wsdl:output message="tns:PurchaseOrderService_getPurchaseOrderResponse"/>
<wsdl:fault name="ServiceException" message="errors:ServiceException"/>
</wsdl:operation>
:

<wsdl:service name="PurchaseOrderService">
<wsdl:port name="PurchaseOrderServiceSoapHttpPort" binding="tns:PurchaseOrderServiceSoapHttp">
<soap:address location="https://efqf-test.prc.us6.oraclecloud.com:443/prcPoEditDocumentOrder/PurchaseOrderServiceV2"/>
:

现在创建服务器并运行 RESTful 和 Soap 端点

[服务器.js]

var fs = require('fs');
var config = require('./config');
var app = require('./app');
var debug = require('debug')('po-service:server');
var http = require('http');
var https = require('https');
var soap = require('soap');
const poService = require('./services/poservice');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || config.get('app.port'));
app.set('port', port);

/**
* Create HTTP server or HTTPS server
*/
var server = http.createServer(app);
if(config.get('app.https')) {
server = https.createServer({
key: fs.readFileSync(config.get('app.serverkey')),
cert: fs.readFileSync(config.get('app.servercert'))
}, app);
}

/**
* Listen on provided port, on all network interfaces.
*/
function startServer() {
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
soap.listen(server, '/prcPoEditDocumentOrder/PurchaseOrderServiceV2', poService.service, poService.xml);
}

if(!module.parent) {
// Start server if file is run directly
startServer();
} else {
// Export server, if file is referenced via cluster
module.exports = startServer;
}

/**
* Normalize a port into a number, string, or false.
*/
function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}
if (port >= 0) {
// port number
return port;
}
return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}

对于配置,我有配置模块

[配置/index.js]

var PropertiesReader = require('properties-reader');
var config = new PropertiesReader('app.properties');

module.exports = config;

下面是配置文件

[应用程序属性]

[app]
hostname = localhost
port = 8080
https = false
serverkey = /path/to/signed.key
servercert = /path/to/signed.crt

现在验证 RESTful 端点

$ curl http://localhost:8080/
{"data":"some data"}

通过高级 REST 客户端验证 Soap 端点

enter image description here

或通过 SoapUI

enter image description here

检查发布的 wsdl 可以通过浏览器检索

enter image description here

关于node.js - 带有 Express.js 的 SOAP 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33062026/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com