Search code examples
node.jsexpressroutescontrollerejs

Unable to Log Outside of app.js in an Express Application


I'm working on an Express application using Node.js, and I'm facing an issue with generating logs outside the app.js file. Here is my setup:

In my app.js file, I'm able to generate logs as expected. For example, I have a /login route where I can use console.log to display messages in the console:

//app.js
const express = require('express');
const db = require('./config/db');
const session = require('express-session');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const bcrypt = require('bcrypt');
const userRoutes = require('./routes/userRoutes');
const traningRoutes = require('./routes/traningRoutes');
const authRoutes = require('./routes/authRoutes');
const flash = require('connect-flash');


const app = express();

// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(express.static('public'));

app.set('view engine', 'ejs');
app.use('/public', express.static(__dirname + '/public'));

app.get('/login', (req, res) => {
  console.log("Here it works");
  res.render('login'); 
});

However, in my dashboard.js file (which is a controller used to handle certain routes), the checkLogin function doesn't generate logs, even though I've added logging statements:

// controllers/dashboard.js
const db = require('../config/db');
const bcrypt = require('bcrypt');


exports.checkLogin = () =>{
  console.log("ici ça fonctionne pas rien n'est log"); // ça ça fonctionne pas 
}
//authRoutes.js
const express = require('express');
const passport = require('passport');
const dashboardController = require('../controllers/dashboardController');

const router = express.Router();

router.get('/login',dashboardController.checkLogin);

I am using the controller-route-views architecture. Can anyone provide insights into why I can't generate logs outside of the app.js file? How can I resolve this issue?

Any help would be greatly appreciated. Thank you!

I want to be able to log out of my app.js


Solution

  • I found where my problem came from is that i wanted to create groups with /api :

    app.use('/api', traningRoutes);
    app.use('/api', authRoutes);
    app.use('/api', userRoutes);
    

    it block all routes so I just remove api and it works

    app.use('/', traningRoutes);
    

    And the most important things it's I have to put :

    app.set('view engine', 'ejs');
    app.use('/public', express.static(__dirname + '/public'));
    // Routes pour les API
    app.use('/', traningRoutes); // This routes must be before 
    app.use('/', authRoutes);//    app.get ('/display'){...}
    app.use('/', userRoutes);//
    
    // Routes
    app.get('/', (req, res) => {
      res.send('Welcome to your CRM Mix Application');
    });
    
    app.get('/login', (req, res) => {
      res.render('login'); 
    });
    
    app.get('/register', (req, res) => {
      res.render('register'); 
    });
    
    app.get('/display', (req, res) => {
      res.render('display'); 
    });
    

    if you put app.get before app.use it will throw

    undefined error