Untitled
unknown
javascript
2 years ago
1.9 kB
3
Indexable
const express = require("express"); const bodyParser = require("body-parser"); const xml2js = require("xml2js"); const soap = require("soap"); const app = express(); app.use(bodyParser.text({ type: "text/xml" })); const service = { Calculator: { CalculatorService: { add: (args) => { const { a, b } = args; return { result: a + b }; }, subtract: (args) => { const { a, b } = args; return { result: a - b }; }, multiply: (args) => { const { a, b } = args; return { result: a * b }; }, divide: (args) => { const { a, b } = args; return { result: a / b }; }, }, }, }; app.post("/calculator", (req, res) => { soap.listen(app, "/calculator", service, xml => { const parser = new xml2js.Parser(); parser.parseString(xml, (err, result) => { const { Body } = result["soap:Envelope"]; const keys = Object.keys(Body); const operation = keys[0]; const args = Body[operation]["0"]; const response = service.Calculator.CalculatorService[operation](args); const builder = new xml2js.Builder(); const xmlResponse = builder.buildObject({ "soap:Envelope": { "$": { "xmlns:soap": "http://schemas.xmlsoap.org/soap/envelope/", "soap:encodingStyle": "http://schemas.xmlsoap.org/soap/encoding/", }, "soap:Body": [ { [`${operation}Response`]: [ response, ], }, ], }, }); res.status(200).type("text/xml").send(xmlResponse); }); }); }); const port = process.env.PORT || 3000; app.listen(port, () => { console.log(`SOAP service listening on http://localhost:${port}/calculator`); });
Editor is loading...