re: Build a REST API with Node, Express & MongoDB! VIEW POST


How can I solve a Corss origin?, I'm calling localhost:3000/subscribers/ endpoint through react application which is running on localhost:3001


Hi, Vicente ! You can solve cross origin setting a header in your server definition.

res.header("Access-Control-Allow-Origin", "YOUR-DOMAIN");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); 

But if you want you can delegate that to a lib called "cors" that you can install:

And that you can use simply by calling it as a Middleware.

var express = require('express')
var cors = require('cors')
var app = express()


It is important to remember that allowing cors without specifying a known domain could be dangerous since anyone would be able to reach your api, be careful and if you have any questions don't hesitate to ask :)

saludos, paisano !


Hey thanks man for some reason I was missing put my domain there, but now it's work fine.

Gracias por el support carnal =).

code of conduct - report abuse