express 设置允许跨域访问

时间:2023-03-09 17:37:54
express 设置允许跨域访问
//demo
const express = require('express');
const app = express();
//设置允许跨域访问该服务. app.all(’’, function (req, res, next) { res.header(‘Access-Control-Allow-Origin’, '’); //Access-Control-Allow-Headers ,可根据浏览器的F12查看,把对应的粘贴在这里就行 res.header(‘Access-Control-Allow-Headers’, ‘Content-Type’); res.header(‘Access-Control-Allow-Methods’, ‘*’); res.header(‘Content-Type’, ‘application/json;charset=utf-8’); next(); }); app.get(’/hello’, function (req, res) { res.json({}) }); const server = app.listen(8082, function () { console.log(‘Express app server listening on port %d’, server.address().port); });