Search code examples
javascriptnode.jsexpressviewserver

Express server not hanging when executing a get request


Hello every one i hope you are doing well please could anyone help me with this issue i don't know what's the problem am new to Express and node trying to created a learning journal I created the normal pages and everyone starts fine once is tarted to create the admin pages at some point nothing is working anymore when i do any request localhost/PORT isn't working localhost/PORT/admin nothing seems to work here is my code for the app.js (main ) and my routes handlers which i keep in a folder called routes in the same directory please help me guys i really need it

In the image attatched the working tree

There is no error in the console so i expected everythin to work and the pages to render nrmally but nothing happens no pages is renders and the callbacks functions of the get requests aren't executed i checked this with a simple console log

const express = require("express");
const mongoose = require("mongoose");
const expressLayout = require("express-ejs-layouts");
const bodyParser = require("body-parser");
//help us to store our login cession so we don't have to log in everytimeconst
cookieParser = require("cookie-parser");
const session = require("express-session");
const MongoStore = require("connect-mongo");
const Blog = require("./models/blog");
const User = require("./models/user");

mongoose.set("strictQuery", false);
/*when you perform queries using Mongoose,you can specify fields that are not defined in the schema,and Mongoose will not throw an error if these fields are presentin the documents being queried. */

const app = express();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(cookieParser);

if (process.env.NODE_ENV !== "production") {
  require("dotenv").config();
}

const PORT = process.env.PORT || 3000;

const CONNECTION = process.env.CONNECTION;

app.use(
  session({
    secret: "keyboard cat",
    resave: false,
    saveUninitialized: true,
    store: MongoStore.create({ mongoUrl: CONNECTION }),
  })
);

app.use(express.static("../public"));

//set a public folder

app.use(expressLayout);
app.set("layout", "./layouts/main");
app.set("view engine", "ejs");

//link to the routes files

app.use("/", require("./routes/main"));
app.use("/", require("./routes/admin"));

app.use((req, res) => {
  res.type("text/plain");
  res.status(404);
  res.send("404 - Not Found");
});
app.use((err, req, res, next) => {
  console.error(err.stack);
  res.status(500).send("Something broke!");
});

const start = async () => {
  try {
    await mongoose.connect(CONNECTION);
    app.listen(PORT, () => {
      console.log("server is running on port" + PORT);
    });
  } catch (e) {
    console.log(e.message);
  }
};

start();

// main.js (routes for all users ) 

const express = require("express");
const router = express.Router();
const mongoose = require("mongoose");
const Blog = mongoose.model("Blog"); // Assuming you've defined your Blog model
// blogs data
//routes
/* GET HOME  */
router.get("", async (req, res) => {
  const locals = {
    title: "Home Page",
  };
  try {
    console.log("am here");
    let perPage = 8;
    let page = req.query.page || 1;
    //const data = await Blog.find()
    const data = await Blog.aggregate([{ $sort: { createdAt: -1 } }])
      .skip(perPage * page - perPage)
      .limit(perPage)
      .exec();

    const count = await Blog.count();
    const nextPage = parseInt(page) + 1;
    const hasNextPage = nextPage <= Math.ceil(count / perPage);

    res.render("index", {
      data,
      locals,
      current: page,
      nextPage: hasNextPage ? nextPage : null,
    });
    // Fetch all blogs from the database
  } catch (error) {
    console.error(error);
    res.status(500).send("Internal Server Error");
  }
});
/* GET ABOUT */
router.get("/about", async (req, res) => {
  const locals = {
    title: "About Page",
  };
  const data = await Blog.aggregate([{ $sort: { createdAt: -1 } }]);
  res.render("about", { data, locals });
});

/* GET Blog:id */
router.get("/blog/:id", async (req, res) => {
  try {
    let slug = req.params.id;
    const data = await Blog.findById({ _id: slug });
    const locals = {
      title: data.title,
    };
    res.render("blog", { data, locals });
  } catch (error) {
    console.error(error);
    res.status(500).send("Internal Server Error");
  }
});

//export the router
module.exports = router;
// Admin.js

const express = require("express");
const router = express.Router();
const mongoose = require("mongoose");
const Blog = mongoose.model("Blog"); // Assuming you've defined your Blog model
const User = mongoose.model("User");
const adminLayout = "../views/layouts/admin";
//this is will help us decript the password so we can store it in the db
const bcrypt = require("bcrypt");
//you should store plain text passwords in ur db
const jwt = require("jsonwebtoken");
/*
 * GET /
 * Admin-Login Page
 */

router.get("/admin", async (req, res) => {
  try {
    const locals = {
      title: "Admin Page",
    };

    res.render("admin/login", { locals, layout: adminLayout });
  } catch (error) {
    console.error(error);
  }
});
router.post("/admin", async (req, res) => {
  const { username, password } = req.body;
  console.log(username);
  console.log(password);
  const hashedPassword = await bcrypt.hash(password);
  try {
    const user = await User.create({ username, hashedPassword });
    res.status(201).json({ message: "User Created", user });
  } catch (error) {
    if (error.code === 11000) {
      res.status(409).json({ message: "User already in use" });
    }
    res.status(500).json({ message: "Internal server error" });
  }
  res.redirect("/admin");
});

module.exports = router;

Solution

  • You have dependency problem here:

    if (process.env.NODE_ENV !== "production") {
      require("dotenv").config();
    }
    

    Your if() statement relies on the process.env.NODE_ENV variable being available to compare against the string production. However, in order to get access to NODE_ENV value you first need to import the dotenv module. Well, in your case you don't import it unless the NODE_ENV is equal to production. You see the problem?

    The knock on effect is that your listener never executes because here:

    const CONNECTION = process.env.CONNECTION;
    

    CONNECTION will be undefined because you haven't been able to import dotenv earlier. So when you try to use CONNECTION here:

    await mongoose.connect(CONNECTION);
    app.listen(PORT, () => {
       console.log("server is running on port" + PORT);
    });
    

    It should throw an error.

    Simply move the require("dotenv").config(); statement out of the if() statement.