I deployed 2 apps to Heroku following tutorials for MERN stack application development and deployment, and both times, when I open the app in the browser via Heroku's "Open App" link, I get a server error displayed called 405 and it renders text to the page that says "Method Not Allowed."
There's not any information for me to go on other than that 405 error, so I'm not sure how to debug it. In the Heroku CLI, it said my deployment was successful, but clearly there's something wrong or I'd be able to see my application at this URL.
I've tried Googling solutions but so far I've just seen a lot of people fix this error but not really describe in much detail what they did to fix it. I've looked at more recent tutorials from 2019 to see if I did something wrong in setup or with Git, but so far everybody's apps seem to work but mine. I even added a Procfile. I read that this error might be API-related, so here's my routes file.
const express = require('express');
const router = express.Router();
const Item = require('../../models/Item');
router.get('/', (req, res) => {
Item.find()
.sort({ date: -1 }) //Sort API in descending order
.then(items => res.json(items))
});
router.post('/', (req, res) => {
const newItem = new Item({
name: req.body.name
})
newItem.save().then(item => res.json(item));
})
router.delete('/:id', (req, res) => {
Item.findById(req.params.id)
.then(item => item.remove().then(() => res.json({deletedItem:
true})))
.catch(err => res.status(404).json({deletedItem: false}));
})
module.exports = router;
And here's the server package.json:
{
"name": "mern_shopping_list",
"version": "1.0.0",
"description": "Shopping list built with MERN stack",
"main": "server.js",
"scripts": {
"client-install": "cd client && npm i",
"start": "node server.js",
"server": "nodemon server.js",
"client": "cd client npm start",
"dev": "concurrently \"npm run server\" \"npm run client\"",
"heroku-postbuild": "cd client && npm install && npm run build"
},
"author": "Steven Boutcher",
"license": "MIT",
"dependencies": {
"concurrently": "^4.1.1",
"dotenv": "^8.0.0",
"express": "^4.17.1",
"mongoose": "^5.6.4"
},
"engines":{
"node": "10.15.0",
"npm": "6.4.1"
},
"devDependencies": {
"nodemon": "^1.19.1"
}
}