按照用于MERN堆栈应用程序开发和部署的教程,我向Heroku部署了2个应用程序,两次,当我通过Heroku的“打开应用程序”链接在浏览器中打开应用程序时,我收到显示为405的服务器错误,并呈现文字显示为“不允许使用方法”页面。
除了405错误外,我没有其他需要继续的信息,所以我不确定如何调试它。在Heroku CLI中,它表示我的部署已成功,但是很明显有问题,或者我可以在此URL上查看我的应用程序。
我已经尝试使用Google搜索解决方案,但到目前为止,我只是看到很多人都在解决此错误,但并未真正详细地描述他们为解决此错误所做的工作。我查看了2019年以来的最新教程,以了解我在设置或使用Git时是否做错了什么,但到目前为止,每个人的应用程序似乎都能正常工作,但我的却是。我什至添加了一个Procfile。我读到这个错误可能与API有关,所以这是我的路由文件。
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;
这是服务器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"
}
}