Moment For Technology

Node.js + Express solves cross-domain problems

Posted on Jan. 29, 2023, 12:10 p.m. by 鞠龍
Category: The back-end Tag: express


Open app.js file in var app = express(); Add this code below

app.all("*", (req, res, next) = {
  res.setHeader("Access-Control-Allow-Origin", "*");
  res.setHeader("Access-Control-Allow-Headers", "*");
  res.setHeader("Access-Control-Allow-Methods", "PUT,POST,GET,DELETE,OPTIONS");
  res.setHeader("Access-Control-Allow-Credentials", "true")
Copy the code

Pay attention to

Var app = express(); It is even better to add the above snippet after this line

About (Moment For Technology) is a global community with thousands techies from across the global hang out!Passionate technologists, be it gadget freaks, tech enthusiasts, coders, technopreneurs, or CIOs, you would find them all here.