提问人:Dzy 提问时间:8/12/2023 最后编辑:Dzy 更新时间:8/12/2023 访问量:462
Vercel 部署无服务器函数已崩溃
Vercel deploy serverless function has crashed
问:
嗨,大家好,我正在尝试将我的 MERN 应用程序部署到 vercel,但是当我尝试部署后端时遇到问题,即使我在本地服务器上尝试时根本没有错误
你能帮我解决这个问题吗?
**这里是错误:**
This Serverless Function has crashed.
Your connection is working correctly.
Vercel is working correctly.
500: INTERNAL_SERVER_ERROR
Code: FUNCTION_INVOCATION_FAILED
我的index.js :
import express from "express"
import mongoose from "mongoose"
import cors from "cors";
import dotenv from "dotenv";
import UserRoute from "./routes/UserRoute.js"
import ContentRoutes from "./routes/contentRoute.js";
import MessageRouter from "./routes/MessageRoute.js"
import connectDB from "./mongodb/connect.js";
dotenv.config();
const app = express();
mongoose.connect
app.use(cors(
{
origin: {"https://hotel-backend-xi.vercel.app"},
methods: {"POST", "GET", "DELETE", "PUT"},
credentials: true
}
));
app.use(express.json({ limit: "25mb" }));
app.use(UserRoute);
app.use(ContentRoutes);
app.use(MessageRouter);
app.get('/', (req, res) => {
res.send("hello hotel")
console.log("Hello hotel")
})
const startServer = () => {
try {
connectDB(process.env.MONGODB_URL)
app.listen(5000, () => {
console.log("Server listening on 5000 http://localhost:5000");
});
} catch (err) {
console.log(err)
}
}
startServer();
这是我的依赖项(package.json):
{ "name": "backend", "version": "1.0.0", "description": "", "type": "module", "main": "index.js", "scripts": { "start": "nodemon index", "test": "echo \"Error: no test specified\" && exit 1" }, "keywords": [], "author": "", "license": "ISC", "dependencies": { "bcrypt": "^5.1.0", "bcryptjs": "^2.4.3", "cloudinary": "^1.37.3", "cors": "^2.8.5", "dotenv": "^16.3.1", "express": "^4.18.2", "joi": "^17.9.2", "joi-password-complexity": "^5.1.0", "jsonwebtoken": "^9.0.1", "mongoose": "^7.3.1", "multer": "^1.4.5-lts.1", "uuid": "^9.0.0" } }
我尝试了几种方法,但没有用 我希望你能帮我解决这个问题
答:
0赞
Zac Anger
8/12/2023
#1
您尝试运行的是 Web 服务器,而不是无服务器函数。您可以将 Express 应用程序作为函数运行,但您当前拥有的应用程序需要在传统服务器上运行,而不是 Vercel。
评论