问题
I am having issues with deploying my heroku app (I have actually been trying to deploy this for a week trying everything I find as a solution). I set up all the development environments as needed but it seems like once the app is deployed the static files are not being served. Once I deploy my app, I get the following: Cannot GET /. In the console I see: Failed to load resource: "the server responded with a status of 404 (Not Found)", as well as: "Refused to load the image 'https://freshgear.herokuapp.com/favicon.ico' because it violates the following Content Security Policy directive: "default-src 'none'". Note that 'img-src' was not explicitly set, so 'default-src' is used as a fallback."
Any asistance will be much appreciated!
I will post my code to give some context:
This is what I have in 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));
This is what I have in 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)
}
})
});
}
this is my package.json file:
{
"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"
}
}
This is my file folder if it helps:
Also, this is what I see when I run the tail:
2020-03-12T02:17:12.701800+00:00 app[web.1]: App running on port 6689... 2020-03-12T02:17:13.112184+00:00 heroku[web.1]: State changed from starting to up 2020-03-12T02:17:13.020682+00:00 app[web.1]: DB connection successful! 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 service=18ms status=404 bytes=598 protocol=https
来源:https://stackoverflow.com/questions/60647361/error-when-deploying-heroku-app-cannot-get