问题描述
我正在尝试使用 Heroku 部署快速服务器,但出现错误 H10 我尝试了很多方法来解决它,尤其是它可能在本地服务器中的工作,但在 Heroku 中,我无法在索引文件中看到我需要的文件。 这是错误,任何帮助,请
`
2020-12-21T09:35:42.994454+00:00 app[web.1]:
2020-12-21T09:35:43.212655+00:00 app[web.1]: [nodemon] 2.0.6
2020-12-21T09:35:43.213503+00:00 app[web.1]: [nodemon] to restart at any time,enter `rs`
2020-12-21T09:35:43.213966+00:00 app[web.1]: [nodemon] watching path(s): *.*
2020-12-21T09:35:43.214061+00:00 app[web.1]: [nodemon] watching extensions: js,mjs,json
2020-12-21T09:35:43.214535+00:00 app[web.1]: [nodemon] starting `node index.js`
2020-12-21T09:35:43.857962+00:00 app[web.1]: internal/modules/cjs/loader.js:983
2020-12-21T09:35:43.857979+00:00 app[web.1]: throw err;
2020-12-21T09:35:43.857980+00:00 app[web.1]: ^
2020-12-21T09:35:43.857980+00:00 app[web.1]:
2020-12-21T09:35:43.858000+00:00 app[web.1]: Error: Cannot find module './routes/Posts'
2020-12-21T09:35:43.858001+00:00 app[web.1]: Require stack:
2020-12-21T09:35:43.858001+00:00 app[web.1]: - /app/index.js
2020-12-21T09:35:43.858004+00:00 app[web.1]: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:980:15)
2020-12-21T09:35:43.858005+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:862:27)
2020-12-21T09:35:43.858006+00:00 app[web.1]: at Module.require (internal/modules/cjs/loader.js:1042:19)
2020-12-21T09:35:43.858006+00:00 app[web.1]: at require (internal/modules/cjs/helpers.js:77:18)
2020-12-21T09:35:43.858007+00:00 app[web.1]: at Object.<anonymous> (/app/index.js:10:21)
2020-12-21T09:35:43.858007+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1156:30)
2020-12-21T09:35:43.858007+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1176:10)
2020-12-21T09:35:43.858008+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:1000:32)
2020-12-21T09:35:43.858008+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:899:14)
2020-12-21T09:35:43.858009+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
2020-12-21T09:35:43.858010+00:00 app[web.1]: at internal/main/run_main_module.js:18:47 {
2020-12-21T09:35:43.858010+00:00 app[web.1]: code: 'MODULE_NOT_FOUND',2020-12-21T09:35:43.858011+00:00 app[web.1]: requireStack: [ '/app/index.js'
]
2020-12-21T09:35:43.858011+00:00 app[web.1]: }
2020-12-21T09:35:43.886850+00:00 app[web.1]: [nodemon] app crashed - waiting
for file changes before starting...
2020-12-21T09:36:39.236547+00:00 heroku[web.1]: Error R10 (Boot timeout) -> Web process Failed to bind to $PORT within 60 seconds of launch
2020-12-21T09:36:39.265465+00:00 heroku[web.1]: Stopping process with SIGKILL2020-12-21T09:36:39.312803+00:00 app[web.1]: Error waiting for process to terminate: No child processes
2020-12-21T09:36:39.382509+00:00 heroku[web.1]: Process exited with status 222020-12-21T09:36:39.433719+00:00 heroku[web.1]: State changed from starting to crashed`
`
########### index.js
const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const app = express();
require("dotenv").config()
const PostsRoutes = require("./routes/Posts");
app.use(bodyParser.json({ limit: "30mb",extended: true }));
app.use(bodyParser.urlencoded({ limit: "30mb",extended: true }));
app.use(cors());
app.use("/posts",PostsRoutes);
app.get('/',(req,res)=>{
res.send('Hello fnh ')
})
const port = process.env.PORT || 5000;
mongoose
.connect(process.env.CONNECTION_URL,{
useNewUrlParser: true,useUnifiedTopology: true,})
.then(() =>
app.listen(port,() => console.log(`Server running on port`))
)
.catch((error) => console.log(error.message));
mongoose.set("useFindAndModify",false);
const mongoose = require("mongoose");
const { update } = require("../models/postMessages");
const PostMessage = require("../models/postMessages");
module.exports = {
getPosts: async (req,res) => {
try {
const postMessages = await PostMessage.find();
res.status(200).json(postMessages);
} catch (error) {
res.status(404).json({ message: error.message });
}
},createPost: async (req,res) => {
const { title,message,selectedFile,creator,tags } = req.body;
const newPostMessage = new PostMessage({
title,tags,});
try {
await newPostMessage.save();
res.status(201).json(newPostMessage);
} catch (error) {
res.status(404).json({ message: error.message });
}
},updatePost : async (req,res) => {
console.log('fnnnh')
const { id } = req.params;
const { title,tags } = req.body;
if (!mongoose.Types.ObjectId.isValid(id)) return res.status(404).send(`No post with id: ${id}`);
const updatedPost = { creator,title,_id: id };
await PostMessage.findByIdAndUpdate(id,updatedPost,{ new: true });
res.json(updatedPost);
},deletePost:async (req,res) => {
console.log('fnnnhh44')
const { id } = req.params;
if (!mongoose.Types.ObjectId.isValid(id)) return res.status(404).send(`No post with id: ${id}`);
await PostMessage.findByIdAndRemove(id);
res.json({ message: "Post deleted successfully." });
},likePost: async (req,res)=>{
const {id}=req.params
if (!mongoose.Types.ObjectId.isValid(id)) return res.status(404).send(`No post with id: ${id}`);
const post = await PostMessage.findById(id);
const updatedPost= await PostMessage.findByIdAndUpdate(id,{likeCount: post.likeCount + 1},{new:true})
res.json(updatedPost)
}
};
######### routes/posts.js
const express = require("express");
const router = express.Router();
const postscontroller = require("../controllers/posts");
router.get("/",postscontroller.getPosts);
router.post("/",postscontroller.createPost);
router.patch("/:id",postscontroller.updatePost);
router.delete("/:id",postscontroller.deletePost);
router.patch("/:id/likePost",postscontroller.likePost);
module.exports = router;
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)