我正在努力使用 Express with Helmet 来提供使用“create-react-app”创建的构建。我在资源管理器控制台中收到几个与内容安全策略相关的错误:
当然,它没有显示应用程序。我注意到如果在 Express 中删除 Helmet 作为中间件,它可以工作,但这不是我想要的解决方案。这是我的服务器代码:
const express = require('express');
const helmet = require('helmet');
const cors = require('cors');
const morgan = require('morgan');
const bodyParser = require('body-parser');
/**
* Server Configuration
*/
const whitelist = [];
const app = express();
// Express Configurations
// Enable reverse proxy support in Express. This causes the the "X-Forwarded-Proto" header field to be trusted so its
// value can be used to determine the protocol. See // http://expressjs.com/api#app-settings for more details.
app.enable('trust proxy');
app.use(morgan('dev')); // Log every request to the console
app.use(helmet()); // Configure secure Headers
app.use(bodyParser.urlencoded({ extended: false })); // Enable parsing of http request body
app.use(bodyParser.json());
// CORS Configuration
const corsOptions = {
origin: (origin, callback) => {
if (whitelist.indexOf(origin) !== -1 || !origin) {
callback(null, true);
} else {
callback(new Error('Not allowed by CORS'));
}
},
};
app.use(cors(corsOptions)); // Allow CORS
/**
* Launcher method
*/
app.start = () => {
// start node server
const port = process.env.PORT || 3000;
app.listen(port, () => {
console.log(`App UI available http://localhost:${port}`);
console.log(
`Swagger UI available http://localhost:${port}/swagger/api-docs`,
);
});
};
/**
* App Initialization
*/
function initializeApp(readyCallback) {
readyCallback(null, app);
}
module.exports = (readyCallback) => {
initializeApp(readyCallback);
};
谁能帮我一把?提前致谢!