我在部署heroku应用程序时遇到问题(实际上,我一直在尝试部署它一个星期,以尝试找到的所有解决方案)。我根据需要设置了所有开发环境,但是似乎一旦部署了该应用程序,就不会再提供静态文件。部署应用程序后,将得到以下信息:无法获取/。在控制台中,我看到:无法加载资源:“服务器响应状态为404(未找到)”,以及:“拒绝加载图像'https://freshgear.herokuapp.com/favicon.ico',因为它违反了以下内容安全性策略指令:“ default-src'none'”。请注意,未明确设置'img-src',因此将'default-src'用作后备。”
任何帮助将不胜感激!
我将发布代码以提供一些上下文:
这就是我在server.js中拥有的
const DB = process.env.DATABASE.replace( '<PASSWORD>', process.env.DATABASE_PASSWORD );
mongoose .connect(DB, {
useNewUrlParser: true,
useCreateIndex: true,
useUnifiedTopology: true }) .then(() => console.log('DB connection successful!')) .catch(err => console.log(err));
这是我在app.js中拥有的:
if (process.env.NODE_ENV === 'production') {
app.use(express.static(path.join(__dirname, 'client/build')), function (err) {
if (err) {
res.status(500).send(err)
}
});
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'client', 'build', 'index.html'), function (err) {
if (err) {
res.status(500).send(err)
}
})
});
}
这是我的package.json文件:
{
"name": "clear",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"client": "cd client && npm start",
"server": "nodemon server.js",
"build": "cd client && npm run build",
"dev": "concurrently --kill-others-on-fail \"npm run server\" \"npm run client\"",
"start": "node server.js",
"heroku-postbuild": "cd client && npm install && npm install --only=dev --no-shrinkwrap && npm run build"
},
"author": "",
"license": "ISC",
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.19.0",
"compression": "^1.7.4",
"concurrently": "^5.1.0",
"cookie-parser": "^1.4.4",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-favicon": "^2.0.1",
"express-mongo-sanitize": "^1.3.2",
"express-rate-limit": "^5.0.0",
"helmet": "^3.21.1",
"hpp": "^0.2.2",
"html-to-text": "^5.1.1",
"jsonwebtoken": "^8.5.1",
"mongodb": "^3.5.4",
"mongoose": "^5.7.1",
"morgan": "^1.9.1",
"multer": "^1.4.2",
"ndb": "^1.1.5",
"nodemailer": "^6.3.0",
"npm": "^6.4.1",
"path": "^0.12.7",
"pug": "^2.0.4",
"slugify": "^1.3.5",
"stripe": "^7.9.1",
"validator": "^11.1.0",
"xss-clean": "^0.1.1"
},
"engines": {
"node": "10.15.3",
"npm": "6.10.3"
},
"devDependencies": {
"cross-env": "^7.0.0",
"concurrently": "^4.0.1"
}
}
如果有帮助,这是我的文件夹:
此外,这是我跑尾巴时看到的:
2020-03-12T02:17:12.701800 + 00:00 app [web.1]:应用程序在端口6689上运行... 2020-03-12T02:17:13.112184 + 00:00 heroku [web.1]:状态从启动更改为启动 2020-03-12T02:17:13.020682 + 00:00 app [web.1]:数据库连接成功! 2020-03-12T02:43:48.761028 + 00:00 heroku [router]:at = info method = GET path =“ /” host = freshgear.herokuapp.com request_id = 488bb9cb-5872-4bbc-bce6-e0db2446743b fwd =“ 45.48.78.101“ dyno = web.1 connect = 0ms服务= 18ms状态= 404字节= 598协议= https