Vercel 部署无服务器功能崩溃了

问题描述 投票:0回答:1

大家好,我正在尝试将我的 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"
  }
}

我尝试了多种方法,但都不起作用 我希望你能帮我解决这个问题

deployment mern server-side vercel
1个回答
0
投票

Vercel 适用于“无服务器功能”,而不是长时间运行的应用程序。您可以将 Express 应用程序作为函数运行,但您当前所拥有的内容需要在传统服务器上运行。

© www.soinside.com 2019 - 2024. All rights reserved.