Putting node GET function in POST function causes 404 Error

Viewed 19

I'm trying to make an api call and I need to POST data then subsequently GET that data. In order for my data to be displayed I need to put the app.get inside of an app.post function, but in turn this ends up giving me an error of 404 localhost NOT FOUND.

Whenever I take the app.get out of the POST function it works fine. How can I prevent this error and fix this?

App.js

app.use(bodyParser.json());

app.use(function(req, res, next) {
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
    next();
  });

app.post("/", jsonParser, async (req, res) => {
    const cardHolder = req.body.cardHolder;
    const balance = req.body.balance;
    console.log(cardHolder);
    console.log(balance);

    // Payload: Flutterwave Card Details
    const payload = {
        "currency": "USD",
        "amount": balance,
        "billing_name": cardHolder,
        "billing_address": "2014 Forest Hills Drive",
        "billing_city": "React",
        "billing_state": "NY",
        "billing_postal_code": "000009",
        "billing_country": "US",
    }

    const createCardResponse = await flw.VirtualCard.create(payload);

    const newPayload = {
        "id": createCardResponse.data.id
    }

    const fetchResponse = await flw.VirtualCard.fetch(newPayload);
    console.log(fetchResponse);

    app.use(function(req, res, next) {
        res.header('Access-Control-Allow-Origin', '*');
        res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
        next();
    });
                
    app.get('/', async (req, res) => {
        res.send("fetchResponse")
    })
});

app.use(bodyParser.json());

app.listen(5000, () => {console.log("Server started on port 5000")})
0 Answers
Related