Express POST Request Java Script:函数调用只能运行一次

时间:2019-06-24 14:47:24

标签: javascript express post

我遇到这个问题,当通过POSTMAN以JSON通过Postman向我的快速应用发送POST请求时,

{ “ eingangswaehrung”:“ EUR”, “ zielwaehrung”:“ AUD”, “ betrag”:100 }

函数调用rechner(req.body.eingangswaehrung,req.body.zielwaehrung.req.body.betrag)仅成功执行一次,再次发送请求时出现此错误:

“错误:您的xml文件中有错误:文档元素后出现垃圾”。

有人可以告诉我为什么吗,以及如何解决这个问题?我仍在学习,但我确信将来会遇到很多此类问题。

谢谢你们!丽莎

var https = require('https');
var xml2js = require('xml2js');
var parser = new xml2js.Parser();
parser.on('error', function (err) { console.log('Parser error', err); });
var jsonparser = require('xml2json');


//Express Server auf Port 8001

'use strict';

var express = require('express');
var app = express();

const port = process.env.PORT || 8001;
app.listen(port, () => console.log(`Server started on port ${port}`))

const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

const cors = require('cors');
app.use(cors());


app.get('/rechner', (req, res) => {
    res.send('get works');
})


app.post('/rechner', (req, res) => {

        const test = async function (a, b) {
            const result = await rechner(req.body.eingangswaehrung, req.body.zielwaehrung, req.body.betrag);
            console.log(result);
        }

        test();
    }

    res.send(req.body);


});


let data = '';
let umrechner = {};

const rechner = async function (eingangswaehrung, zielwaehrung, betrag) {

    return new Promise((resolve, reject) => {

        https.get('https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml?46910adcfa674863cc9c493891c7e689', function (res) {

            if (res.statusCode >= 200 && res.statusCode < 400) {

                res.on('data', function (data_) { data += data_.toString(); });

                res.on('end', function () {

                    let json;
                    json = jsonparser.toJson(data);
                    json = JSON.parse(json);

                    let currencies = json['gesmes:Envelope'].Cube.Cube.Cube

                    for (let i = 0; i < currencies.length; i++) {
                        umrechner[currencies[i].currency] = parseFloat(currencies[i].rate)
                    }

                    if (!betrag || !eingangswaehrung || !zielwaehrung || zielwaehrung === " ") {
                        reject('Es fehlt eine Eingabe');
                    }

                    let eurowert;

                    if (umrechner[eingangswaehrung] !== undefined) {
                        eurowert = betrag / umrechner[eingangswaehrung];
                    }
                    else if (eingangswaehrung === "EUR") {
                        eurowert = betrag * 1.0;
                    }
                    else {
                        reject('Something went wrong');
                    }

                    let zielbetrag;

                    if (umrechner[zielwaehrung] !== undefined) {
                        zielbetrag = eurowert * umrechner[zielwaehrung];
                    }
                    else if (zielwaehrung === "EUR") {
                        zielbetrag = eurowert * 1.0;
                    }
                    else {
                        reject('Something went wrong');
                    }

                    resolve(zielwaehrung + ' ' + zielbetrag.toFixed(2));

                    return zielwaehrung + ' ' + zielbetrag.toFixed(2);

                })

            }

        });
    })
}

0 个答案:

没有答案