Home > Back-end >  localhost:5000 server is just keep loading and not showing ejs template
localhost:5000 server is just keep loading and not showing ejs template

Time:01-22

I am still learning Node.js. I created a server using express documentation and it worked before. But now for a project I have imported some npm packages. like dotenv, http-errors, ejs and others. I created (.env) file and there decleared PORT=5000, import it in my (main.js) file and called app.listen function. So that, it can show my ejs template in the browser but when i hit http://localhost:5000 it just keeps loading but nothing appears. it's not giving any errors in the terminal either, it just keeps loading unless i press ctrl c in my terminal.

main.js

const express = require("express");
const dotenv = require("dotenv");
const mongoose = require("mongoose");
const path = require("path");
const cookieParser = require("cookie-parser");
const { notFoundHandler, errorHandler } = require("./middlewares/common/errorHandler");


const app = express();
dotenv.config();


//database connection
mongoose.set("strictQuery", true);
mongoose.connect(process.env.MONGO_CONNECTION_STRING, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
})
.then(() => console.log("Database Conntcted!"))
.catch(err => console.log(err));

app.use(express.json);
                  
app.use(express.urlencoded({ extended: true }));


app.set("view engine", "ejs");
app.set('views', 'views');

app.use(express.static(path.join(__dirname, "public")));


app.use(cookieParser(process.env.COOKIE_SECRET));


app.use(notFoundHandler);

//common error handler
app.use(errorHandler);


app.listen(process.env.PORT, () => {
  console.log(`app is listing at port ${process.env.PORT}`);
});

my errorhandler.js

const createError = require("http-errors");


function notFoundHandler(req, res, next) {
  next(createError(404, "CONTENT WAS NOT FOUND!"));
}

function errorHandler(err, req, res, next) {
  res.render("error", {
    title: "Error Page",
  });
}

module.exports = {
  notFoundHandler: notFoundHandler,

  errorHandler,
 
};

error.ejs my ejs file route ("./views/error.ejs")

  <title><%= title %></title>
<body>
    Alart!
</body>

.env file

PORT=5000

CodePudding user response:

problem fixed! The problem was in my main file. There was a parentheses() missing.

it would be app.use(express.json());

  • Related