当前位置: 首页 > 工具软件 > Node-SOAP > 使用案例 >

2018 SOAP(2)SAOP with TS module soap-decorators

易昌翰
2023-12-01
2018 SOAP(2)SAOP with TS module soap-decorators

TypeScript SOAP and example
https://github.com/RobinBuschmann/soap-typescript
https://github.com/RobinBuschmann/soap-typescript/tree/master/example
Prepare the ENV
Search “experimentalDecorators” in VSCode and set that to true.

Set Up a Simple Project named services.soapexport, the sample package.json file will be as follow:
{
"author": "Data Team",
"dependencies": {
"@types/autobahn": "^0.9.37",
"@types/chai": "^4.0.4",
"@types/mocha": "^2.2.43",
"@types/node": "^8.0.28",
"@types/sinon": "^2.3.5",
"@types/lodash": "4.14.106",
"autobahn": "^0.12.0",
"aws-sdk": "^2.103.0",
"base-64": "^0.1.0",
"chai": "^4.1.2",
"config": "^1.26.2",
"dynamodb-local": "0.0.18",
"log4js": "^2.3.3",
"redis": "^2.8.0",
"soap-decorators": "^1.0.2",
"request": "^2.73.0",
"request-promise": "^4.1.1",
"sinon": "^3.2.1",
"sinon-test": "^2.1.1",
"typescript": "^2.1.4",
"uuid-validate": "0.0.2",
"express": "^3.20.0",
"express-soap": "1.0.3",
"soap": "0.23.0",
"reflect-metadata": "^0.1.8"
},
"description": "Lifeszie cloud directory microservice",
"devDependencies": {
"del": "^3.0.0",
"gulp": "^3.9.1",
"gulp-env": "^0.4.0",
"gulp-mocha": "^2.2.0",
"gulp-shell": "^0.5.2",
"gulp-tslint": "^4.3.4",
"gulp-typescript": "^3.1.3",
"mocha": "^3.5.3",
"mocha-sinon": "^2.0.0",
"tslint": "^3.13.0"
},
"engines": {
"node": ">=4.1.1"
},
"keywords": [
"typescript",
"soap",
"nodejs"
],
"license": "(c) Sillycat 2016",
"name": "user-microservice",
"scripts": {
"build": "gulp build",
"dev": "NODE_ENV=dev node build/src/server.js",
"beta": "NODE_ENV=stage node build/src/server.js",
"start": "node build/src/server.js",
"test": "gulp test",
"tsc": "tsc",
"client": "node build/test/client.js",
"loglevel": "node build/src/logger.js"
},
"version": "0.0.1"
}

The tsconfig.json and tslint.json will be as follow:
{
"compilerOptions": {
"outDir": "build",
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},
"exclude": [
"node_modules",
"typings"
]
}

{
"rules": {
"class-name": true,
"curly": true,
"eofline": false,
"forin": false,
"indent": [
true,
4
],
"label-position": true,
"label-undefined": true,
"max-line-length": [
true,
140
],
"no-arg": true,
"no-bitwise": true,
"no-console": [
true,
"debug",
"info",
"time",
"timeEnd",
"trace"
],
"no-construct": true,
"no-debugger": true,
"no-duplicate-key": true,
"no-duplicate-variable": true,
"no-empty": false,
"no-eval": true,
"no-string-literal": false,
"no-trailing-whitespace": true,
"no-unused-variable": false,
"no-unreachable": true,
"no-use-before-declare": true,
"one-line": [
true,
"check-open-brace",
"check-catch",
"check-else",
"check-whitespace"
],
"radix": true,
"semicolon": true,
"triple-equals": [
true,
"allow-null-check"
],
"variable-name": false,
"whitespace": [
true,
"check-branch",
"check-decl",
"check-operator",
"check-separator"
]
}
}

Define the Input and Output, CalculatorInput.ts and CalculatorOutput.ts
import {XSDComplexType, XSDElement} from 'soap-decorators';

@XSDComplexType
export class CalculatorInput {

@XSDElement
a: number;

@XSDElement
b: number;

}

import {XSDComplexType, XSDElement} from 'soap-decorators';

@XSDComplexType
export class CalculatorResult {

@XSDElement
value: number;
}

Define the simple Service on top of annotation in CalculatorService.ts
import {SoapService, SoapOperation} from 'soap-decorators';
import {CalculatorInput} from './CalculatorInput';
import {CalculatorResult} from './CalculatorOutput';

@SoapService({
portName: 'CalculatorPort',
serviceName: 'CalculatorService'
})
export class CalculatorService {
@SoapOperation(CalculatorResult)
add(data: CalculatorInput, res: (res: CalculatorResult) => any): void {
res({
value: data.a + data.b
});
}

@SoapOperation(CalculatorResult)
subtract(data: CalculatorInput, res: (res: CalculatorResult) => any): void {
res({
value: data.a - data.b
});
}
}

Start the Service in soap.ts
import * as express from 'express';
import * as http from 'http';
import {CalculatorService} from './CalculatorService';
import {soap} from 'soap-decorators';

export class SoapServer {

private server;

constructor(listenPort: number, address: string) {
}

public async init() {

}

public async start() {
let app = express();
let calculatorService = new CalculatorService();
app.use('/calculatorService', soap(calculatorService));
http.createServer(app)
.listen(3000, () => console.log('SOAP Server listining') );


// let app = express();
// let calculatorService = new CalculatorService();
// let xml = require('fs').readFileSync('/Users/hluo/company/code/services.soapexport/wsdl/calculatorService.wsdl', 'utf8');
// app.listen(3000, function(){
// soap.listen(app, '/calculatorService', calculatorService, xml);
// });
}

}

Directly Use SOAP Package
https://github.com/vpulim/node-soap#basicauthsecurity
https://stackoverflow.com/questions/22884513/simple-webservice-with-node-soap


References:
https://www.npmjs.com/package/soap
https://github.com/benerone/soapStub

https://github.com/vpulim/node-soap
https://github.com/RobinBuschmann/soap-typescript

More example
https://blog.csdn.net/cwallow/article/details/41983467
https://github.com/tan-tan-kanarek/node-soap-server
https://stackoverflow.com/questions/22884513/simple-webservice-with-node-soap
https://stackoverflow.com/questions/20740975/how-to-get-started-node-soap
https://github.com/tan-tan-kanarek/node-soap-server

Directly use the API
https://github.com/vpulim/node-soap#basicauthsecurity
https://stackoverflow.com/questions/22884513/simple-webservice-with-node-soap
 类似资料: