Quantcast
Viewing all articles
Browse latest Browse all 3

Answer by Bjørn Reemer for Node GET request is hanging unable to finish the request and the page keep loading

It seems you are not calling your async mongo function. You are only declaring your function. You need to call it by placing () after your declaration like so:

(async function mongo() {    // Your code})();

So applying that change to your code would result in the following:

const express = require("express");const router = express.Router();const { MongoClient } = require("mongodb");/* GET Books page. */router.route("/").get((req, res, next) => {  const url = "mongodb://localhost:27017";  const dbName = "Library";  (async function mongo() {    let client;    try {      client = await MongoClient.connect(url, { useUnifiedTopology: true });      const db = client.db(dbName);      const books = await db.collection("books").find().toArray();      res.render({        title: "Books",        books,      });    } catch (err) {      console.log(err);    }  })();});module.exports = router;

Because of this your request would indeed timeout as the code within the async function is not called.

Hope this answer was helpful 👍


Viewing all articles
Browse latest Browse all 3

Trending Articles