Home > Enterprise >  console.log() not appearing in output for express server
console.log() not appearing in output for express server

Time:03-08

I can't for the life of me figure out how to get console.log() to appear with my express server. It's a middle-tier API for our front-end. You'll have to forgive me if I speak about it a little awkwardly, I'm relatively inexperienced with these tools but I'll do my best to explain the issue despite my inexperience. I'm trying to use console.log to get a better idea of a rather complex projects behavior and what might be causing some issues with it in its current state. Unfortunately console.log only seems to work within plainjane examples like so:

export const routerExample = express.Router();
    
routerExample.use((req, res, next) => {
    console.log('Time: ', Date.now()); // I show up in console just fine
    next();
});

When I try to lookup the problem I'm experiencing all solutions seem to be regarding getting routing examples like the one above to appear in console, I can see such examples just fine. The problem comes from getting anything to show up in examples like:

// routing.ts
import { homeController } from '../controllers/homeController';

const homeEx: HomeExample = new HomeExample();

routerExample.get('/home', homeEx.getHome);
// homeController.ts
export class HomeExample {
    public getHome (req: Request, res: Response) : void {
        console.log("something is happening");
        // do stuff
    }
}

Any uses of console.log like above never appear anywhere in node's console (or elsewhere as far as I can tell).

What am I missing that is needed to make these log messages appear? This has to be incredibly simple but I've been through numerous similar sounding issues on stackoverflow and elsewhere and everything single of one of them seems to be describing slightly different issues (or misunderstandings) that don't solve my own issue. I've tried numerous versions of node/npm and have added the DEBUG:* flag as well. None of this seems to solve it. If I'm missing any code that'd help give context to the issue let me know. I've obviously cut down parts and renamed some objects as I can't exactly dump work-related code here. What am I missing? Any help would be greatly appreciated :)

Edit 1: since many similar posts to this seem to get this mixed up, no I'm not looking at my front-end's console or something for the output. I'm looking in the terminal window where I start the server from, where the router example does appear.

Edit 2: for reference, my file structure is something like:

  • app/
    • controllers/
      • homeController.ts (HomeExample stuff is here)
    • routes
      • routing.ts (routerExample stuff is here)
    • app.ts
    • ...

Edit 3: the code works overall to be clear. the problem is explicitly that that log.console() isn't appearing, all the code I've wrapped into "// do stuff" is working as expected.

CodePudding user response:

Checkout Express Middlewares

routerExample.get('/home', homeExample);

function homeExample (req: Request, res: Response, next:NextFunction) : void {
         console.log("something is happening");
         // do stuff
     }
}

You are also calling a member of a non static or instantiated class see this: TypeScript - Static

CodePudding user response:

What you are missing is to create a new instance of the homeExample class. What I recommend is to export the new instance on the route file like this:

/routes/home.route.js

class HomeRoute {
  /* your methods */
}

export default new HomeRoute();

then you can use it:

import homeRoutes from './routes/home.route';

router.get('/home', homeRoutes.getHome);

See the example:

https://replit.com/@abranhe/expressjs-console-log#index.js

  • Related