How to receive an array in the body of a nodejs route

Asked

Viewed 589 times

1

Good morning, guys. I’m having a really hard time. I started a week ago studying in nodejs, so I’m well.

I have a code that returns an object in json format to me and I would like to create a page that Body would receive that. but I can’t create at all.

I am using EJS... I did so to receive my route

app.get("/", function(req, res){
    res.render("secao/home");
});

app.listen(3000, function(){
    console.log("Servidor rodando");
});

Then I would like the home to receive the array from here.

var http = require("http");

var options = {
    "method": "GET",
    "hostname": [
    "{{accountName}}",
    "{{environment}}",
    "com",
    "br"
    ],
    "path": [
    "api",
    "catalog_system",
    "pvt",
    "category",
    "tree",
    "{{categoryLevels}}",
    ""
    ],
    "headers": {
        "content-type": "application/json",
        "x-vttt-api-appkey-api-appkey": "{{X-Vtt-API-AppKey}}",
        "x-vttt-api-apptoken": "{{X-Vtt-API-AppToken}}"
    }
};

var req = http.request(options, function (res) {
    var chunks = [];
    res.on("data", function (chunk) {
        chunks.push(chunk);
    });
    res.on("end", function () {
        var body = Buffer.concat(chunks);
        console.log(body.toString());
    });
});
req.end();

How to proceed guys. Excuse my ignorance!!!

1 answer

0

Good morning. You can pass the array directly through the res.render, creating a variable or even a function! First Ex.

app.get('/rota', function(req, res){
     res.render('pagina', {nomeVariavel: nomeArray}); })
})

Second Ex, in your case.

app.get('/', function(req, res){
     res.render('secao/home', {options: options}); })
})

I hope I’ve helped!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.