简体   繁体   中英

How to establish connection to mongo db in a Node js express server using serverless?

I don't know how to establish connection to the mongo db for my node JS server in AWS Lambda using serverless on AWS. I've mentioned my question in the handler function below.

The code looks something like this:

import express from "express";
import mongoose from "mongoose";
import dotenv from "dotenv";   
import cookieParser from "cookie-parser";
import serverless from "serverless-http";

const PORT = 1234;

dotenv.config();

mongoose.connect(
    process.env.MONGO_URL,
    () => {
        console.log("connected to db");
    },
    (err) => {
        console.log({
            error: `Error connecting to db : ${err}`,
        });
    }
);

const app = express();

app.use(cookieParser());
app.use(express.json());

// this part has various routes
app.use("/api/auth", authRoutes);


app.use((err, req, res, next) => {
    const status = err.status || 500;
    const message = err.message || "Something went wrong";
    return res.status(status).json({
        success: false,
        status,
        message,
    });
});

app.listen(PORT, () => {
    console.log(`Server listening on port ${PORT}`);
});

export const handler = () => { 
    // how to connect to mongodb here?
    return serverless(app);
};

Here handler is the AWS lambda's handler function. For each http request I'm reading/writing data from/to my DB in some way. After checking the cloudwatch logs, it was clear that the requests sent to the server result in timeout because the connection to mongodb hasn't been established. So how exactly do I use mongoose.connect here?

I tried doing this:

export const handler = () => { 
    mongoose.connect(
        process.env.MONGO_URL,
        () => {
            console.log("connected to db");
        }
    );
    return serverless(app);
};

But it didn't work possibly because it's asynchronous. So I'm not sure how to make this connection here.

EDIT :

One thing that I realised was that the database server's network access list had only my IP because that's how I set it up initially.

So I changed it to anywhere for now and made the following minor changes:

const connect_to_db = () => {
    mongoose
        .connect(process.env.MONGO_URL)
        .then(() => {
            console.log("Connected to DB");
        })
        .catch((err) => {
            throw err;
        });
};

app.listen(PORT, () => {
    connect_to_db();
    console.log(`Server listening on port ${PORT}`);
});

Now I can see "Connected to DB" in the logs but the requests sent still times out after 15 seconds (the timeout limit set currently).

My logs:

在此处输入图像描述

What am I doing wrong?

So I did some more digging and asked this around the community . Few things that made me understand what I was doing wrong:

  • It appeared I wasn't connecting the db and my app response together. My app was handling the request fine, my db was connecting fine. But there was nothing tying them together. It's supposed to be simple:

    Requests comes in > App waits until db connection has been established > App handles request > App returns response.

  • Second, calling app.listen was another problem in my code. Calling listen keeps the process open for incoming requests and it ultimately is killed by Lambda on timeout.

    In a serverless environment, you don't start a process that listens for requests but, instead, the listening part is done by AWS API Gateway (which I have used to have my Lambda handle http requests) and it knows to send request information to Lambda handler for processing and returning a response. The handler function is designed to be run on each request and return a response.

So I tried adding await mongoose.connect(process.env.MONGO_URL); to all my methods before doing any operation on the database and it started sending responses as expected. This was getting repetitive so I created a simple middleware and this helped me avoid lot of repetitive code.

app.use(async (req, res, next) => {
    try {
        await mongoose.connect(process.env.MONGO_URL);
        console.log("CONNECTED TO DB SUCCESSFULLY");
        next();
    } catch (err) {
        next(err);
    }
});

Another important, but small change. I was assigning lambda's handler incorrectly.

Instead of this:

export const handler = () => { 
    return serverless(app);
};

I did this:

export const handler = serverless(app);

That's it I suppose, these changes fixed my express server on Lambda. If anything I've said is wrong in any way just let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM