Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

306
Vistas
How do i allow 'Access-Control-Allow-Origin' in Node.JS Http Server?

Im trying to send a POST from a client connected to this server to a external URL. I get a lot of 'CORS' errors. Im pretty sure that it's because i haven't allowed 'Access-Control-Allow-Origin' and etc on my Node.JS server. But i don't know how to do it. I have found another answers on the web, but none of them solved my problem. Here is my server:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
const { instrument } = require('@socket.io/admin-ui')
var bodyParser = require('body-parser')
var clientesConectados = [];

app.use( bodyParser.json() );       // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({     // to support URL-encoded bodies
  extended: true
})); 

app.get('/', function(req, res){

  //send the index.html file for all requests
  res.sendFile(__dirname + '/index.html');

});

app.post('/', (request, res) => {
  console.log("********************");
  console.log(" >POST RECEBIDO!!");
 console.log(res);
 
postData = request.body;
io.emit('message',postData); 
 console.log(postData);

});



http.listen(3000, function(){

  console.log('listening on *:3000');

});
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Express has a middleware for CORS. Link here

about 4 years ago · Juan Pablo Isaza Denunciar

0

Install cors package in your application

npm install cors

And then add these two lines in there

var cors = require("cors");
app.use(cors())

Else there are ways that set through header or you could use a proxy in your frontend but using cors package is simpler

about 4 years ago · Juan Pablo Isaza Denunciar

0

Install npm package called cors

After installing, on your file add this line app.use(cors()) will solve your issue of cors error I hope.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda