0

I made a traditional express app for bank-end. I made some couples of api route for front-end. And there is some logic about connecting DB, getting data...etc.

  1. I didn't use serverless function but the first request is very slow. Is it related to the "cold start"? Because i heard that the "cold start" in vercel is related to the serverless function.

  2. So is it not preferred to deploy back-end logic on vercel? (because i think that server-side logic like api request should be "serverful".

app.js file

import express from "express";
import cors from "cors";
import cookieParser from "cookie-parser";
import morgan from "morgan";
import helmet from "helmet";
import postRouter from "./api/post.js";
import { connectDB, disconnectDB } from "./db/connect.js";

const app = express();

await connectDB();

app.use(express.json());
app.use(cookieParser());
app.use(morgan("combined"));
app.use(helmet());

app.use(
  cors({
    origin: "*",
    optionsSuccessStatus: 200,
  })
);

app.use("/api/post", postRouter);

process.on("exit", disconnectDB);

app.listen(8080);

connenct DB logic

import mongoose from "mongoose";

export const connectDB = async () => {
  const uri = `mongodb+srv://hosmimam:${process.env.MONGO_URI}/?retryWrites=true&w=majority`;
  await mongoose.connect(uri, {
    useUnifiedTopology: true,
    useNewUrlParser: true,
  });
};

export const disconnectDB = async () => {
  await mongoose.connection.close();
};
h0lyM0ly1
  • 1
  • 1
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Jun 25 '23 at 07:48

0 Answers0