Express solves cross-domain problems
There are two ways to solve cross-domain express, and I personally recommend the first
1. Installcors
npm install cors --save-dev
const cors = require('cors');
app.use(cors());
Copy the code
2. Manual implementation
app.use((req, res, next) = > {
res.header('Access-Control-Allow-Origin'.The '*')
res.header('Access-Control-Allow-Headers'.'Authorization,X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method' )
res.header('Access-Control-Allow-Methods'.'GET, POST, OPTIONS, PATCH, PUT, DELETE')
res.header('Allow'.'GET, POST, PATCH, OPTIONS, PUT, DELETE')
next();
});
Copy the code