lundi 27 août 2018

Error: Can't set headers after they are sent Ember/Nodejs

I am trying to fetch data from an API, if you see the console ,you will find that a huge chunk of JSON that is variable a2 is displayed in the console .

But I wish to do res.json(a2); to display all the json on the webpage as JSON data.

I am getting an error--

Error: Can't set headers after they are sent.

What do I do >

var express= require('express');
var app=express();
const port= 5000;

var gotit=[];
var Request = require("request");
Request.post({
    "headers": {
        "content-type": "application/json",
        "Authorization": "34ca0e9b-ecdd-4736-a432-d87760ae0926"
    },
    "url": "https://www.lifeguard.insure/v1/quote",
    "body": JSON.stringify({
        "category": "Auto",
        "zipcode": "90293",
        "age": 35,
        "gender": "Male",
        "key": "a2bf34ab-8509-4aa6-aa9e-13ae7917e1b8"
    })
}, (error, response, body) => {
    if (error) {
        return console.dir(error);
    }
    var a2 = (JSON.parse(body));
    console.log(a2)
    gotit=a2;
});

console.log("Gotiit")
console.log(gotit);

    app.get('/api/customers', (req, res) => {
    res.json(gotit);
    });


app.listen(port, () => console.log("Server Started"));




Aucun commentaire:

Enregistrer un commentaire