Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

203
Visualizações
Node JS CORS module not setting Access-Control-Allow-orgin express header
I am trying to create api and fetch it from front but the "Access-Control-Allow-Origin" is not allowing me.

I tried to solve the problem but still showing me the problem.

this is the front part

I fetched it by GET method but not working

 const api = "http://localhost:3000/api/posts/";
        const base_api = "http://localhost:3000/";
        console.log(api);
        window.onload = () => {
          getPost();
        };
        const getPost = () => {
          fetch( api , {
            method: "GET",
          })
            .then((response) => {
              return response.json();
            })
            .then((data) => {
              buildPost(data);
              console.log(data);
            });
        };
        
        const buildPost = (blogPost) => {
          console.log(blogPost);

*this is the express part

I used the "*" that still showing the error. what more solution could be

const express = require("express");
const app = express();
const Post= require("./api/models/posts");
const postsData= new Post();

app.use((req, res, next) =>{
    res.setHeader("Access-Control-Allow-Orgin","*" )
    next();
})

app.use(express.json());

app.use ('/uploads', express.static('uploads'))

app.get("/api/posts", (req, res)=>{

res.status(200).send(postsData.get())
});

app.get("/api/posts/:post_id", (req, res)=>{
    const postId = req.params.post_id;
    const foundedPost= postsData.getEachData(postId);
});
app.listen(3000, ()=> console.log("Listening on

http://localhost:3000/"))

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda