Have a question with NODEJS “CORS Errors”?

March 28, 2019 288 views

I have just worked out a couple of CORS errors (with googling & reading 3 sources) here is what I used (I hope that my spent time will save yours :) )

NodeJS can be configured to use CORS as means of communicating from the front-end. This can be configured in your app.js as middleware for the popular Express package.
CORS errors look something like this:


Both servers are running on different ports. Remember that different port consists as a cross domain request. localhost:8080 is a different domain to localhost:9000 (it’s everything before the forward slash). Node running on port 8080Angular running on port 9000You need to include your CORS enabler before your middleware or app.send requests.

You can either include the following code or include an express middleware package to do this for you, such as npm install CORS.

// Enable CORS
app.use(function( req, res, next ) {
res.header("Access-Control-Allow-Origin", req.headers.origin);
res.header("Access-Control-Allow-Headers", "x-requested-with, content-type");
res.header("Access-Control-Allow-Methods", "GET,PUT,POST,DELETE,OPTIONS");
res.header("Access-Control-Allow-Credentials", "true");
res.header("Access-Control-Max-Age", "1000000000");
// intercept OPTIONS method if ('OPTIONS' == req.method) { res.send(200); } else { next(); } });

Setting the domain in the Angular Seed

You may need to set the domain in the Angular seed go to /app/scripts/config.js Change this line:


To this:


Where the IP address can be your domain name.



who am I > front end, back end, designer & what a hell the boss is needed developer at Buy Essay Club

Be the first one to answer this question.