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') %>