Hope you guys doing good.
I am learning Nodejs where i am trying to create login, signup with mongodb
The issue i am getting is the app is directly opening the 500.ejs file instead of route defined. i am using routes, controller and views
The structure of views is views/auth/login,signup views/404.ejs,500.ejs
The structure of Controller is controller/auth.js, error.js
The structure of route is route.auth.js
Here is my app.js code:
const path = require('path');
const express = require('express');
const bodyParser = require('body-parser');
const mongoose = require('mongoose');
const MONGODB_URI='mongodb+srv://******:**********@******.majjl.mongodb.net/?retryWrites=true&w=majority&appName=******'
const app = express();
// const multer = require('multer')
// Middleware setup
app.set('view engine', 'ejs'); // Set EJS as the template engine
app.set('views', 'views'); // Set the views directory
app.use(bodyParser.urlencoded({ extended: false })); // Parse incoming request bodies
app.use(express.static(path.join(__dirname, 'public'))); // Serve static files from the public directory
const authRoutes = require('./routes/auth')
const errorController = require('./controllers/error');
// Use authentication routes
app.use(authRoutes);
app.use(errorController.get500);
app.use(errorController.get404);
app.use((err, req, res, next) => {
res.status(err.httpStatusCode || 500);
res.render('500', {
statusCode: err.httpStatusCode || 500,
message: err.message || 'Something went wrong!'
});
});
app.get('/', (req, res) => {
res.redirect('/login');
});
mongoose.connect(MONGODB_URI)
.then(result=>{
app.listen(3000);
})
.catch(err=>console.log(err))
Here is my auth controller:
const User = require('../models/user')
const bcrypt = require('bcryptjs')
const crypto = require('crypto');
const { validationResult } = require('express-validator')
const nodemailer = require('nodemailer')
const sendgrindTransport = require('nodemailer-sendgrid-transport')
const transport = nodemailer.createTransport(sendgrindTransport({
auth:{
api_key:"*****************.****************",
}
}))
exports.getLogin = async (req, res, next) => {
let message;
// let message=req.flash('error');
// if(message.length>0)
// {
// message=message[0]
// }else{
// message=null
// }
res.render('auth/login', {
path: '/login',
pageTitle: 'Login',
errorMessage: message,
oldInput : {
email:'',
},
})
}
exports.getSignup = async (req, res, next) => {
let message;
// let message=req.flash('error');
// if(message.length>0)
// {
// message=message[0]
// }else{
// message=null
// }
res.render('auth/signup', {
path: '/signup',
pageTitle: 'Signup',
errorMessage: message,
oldInput : {
fullName:'',
email:'',
},
})
}
Here is my error.js:
exports.get404 = (req, res, next) => {
res.status(404).render('404', { pageTitle: 'Page Not Found', path: '/404',
});
};
exports.get500 = (req, res, next) => {
res.status(500).render('500', { pageTitle: 'Error', path: '/500',
});
};
here is my route/auth.js:
const express = require('express');
const router = express.Router();
const authController = require('../controllers/auth')
const { check, body } = require('express-validator')
const User = require('../models/user')
router.get('/login',authController.getLogin),
router.get('/signup',authController.getSignup),
module.exports = router;
Now when i run the app like npm start the page open and it show 500.ejs with this message:
/Users/mac/development/Private Data/Node Projects/Video-Calling-App/views/500.ejs:6 4| <meta charset="UTF-8"> 5| <meta name="viewport" content="width=device-width, initial-scale=1.0"> >> 6| <title><%= statusCode %> - Error</title> 7| <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"> 8| <style> 9| body { statusCode is not defined
Can anyone please help me to solve this also if i comment the error controller it works fine but after open the login page if i go for /login/chujnd it show cannot get login/chujnd but expectedly it has to show 404 page