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

307
Views
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 answers
Answer question

0

Express has a middleware for CORS. Link here

about 4 years ago · Juan Pablo Isaza Report

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 Report

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 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!