简体   繁体   中英

Node.js: No 'Access-Control-Allow-Origin' header is present on the requested resource

I'm newer to cors and exactly what you need. I had an app working live a couple of months ago and it seems like it has broken due to CORS. Every time I make a request I get the error: "as been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource."

Here is my server cors code:

const express = require("express");
const jwt = require("jsonwebtoken");
const cors = require('cors');

const app = express();

// Set Port
app.set("port", process.env.PORT || 5000);

// Listen
app.listen(process.env.PORT || 5000);

// Load Middlewear
app.use(express.urlencoded({ extended: true }));
app.use(express.json()); // To parse the incoming requests with JSON payloads

// CORS HEADERS MIDDLEWARE
var corsOptions = {
    origin: '*',
    optionsSuccessStatus: 200,
  }
app.use(cors(corsOptions));

You need to allow the back to accept only by the front like this

`` var corsOptions = { origin: 'http://localhost:8080' front ip or dns }

app.use(cors(corsOptions) ``

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM