Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.1K
Views
Extract data from axios get request to render using EJS

So i am trying to extract the data from an API using axios to render on my EJS view:

const axios = require('axios');
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));

app.get("/test", (req, res, next) => {
  let productApi =   axios.get("https://swapi.dev/api/people/1")
  .then(response => res.json(response.data))

  res.render('produtos2', {product: productApi})
})

but i'm getting the erro: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client

if i use it like this:

app.get("/test", (req, res, next) => {
  let productApi =   axios.get("https://swapi.dev/api/people/1")
  .then(response => res.json(response.data))
})

it returns the data, but i can't render the 'produtos2' view using EJS.

My EJS code:

<%- include('partials/header') %>

teste view

<%= product[0] %>

<%- include('partials/footer') %>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

the problem is that you mix res.render and res.json, and that node.js runs asynchronously

so res.render runs before axios request is done, you send a promise object, not data, and when the request is finished, res.json causes ERR_HTTP_HEADERS_SENT error. Also, you don't need res.json at all, only pass the request data to the view.

so, you need to render data after the request is done

in your second code snippet you've started well, there is where you get the data, and then you need to render the view, and instead of res.json, you just pass request data:

app.get("/test", (req, res, next) => {

    axios.get("https://swapi.dev/api/people/1")
        .then(response => {
            res.render('produtos2', {product: response.data });
        });

});

or with commonly used async/await:

app.get("/test", async (req, res, next) => {

    let productApi;

    try {
        productApi = await axios.get("https://swapi.dev/api/people/1")
    } catch(err) {
        console.error(err);
        return res.end('err');
    }

    res.render('produtos2', {product: productApi.data });

});
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!