Robert Brax
Robert Brax

Reputation: 7318

Expressjs randomly hangs, how to output errors?

This is in the context of a nodejs / express 4 app that i'm building locally on MacOsx.

It's a simple app that mostly uses postgresql (in native form) to render pages.

When I browse my pages, login, logout..... at a point the app randomly hangs: browser stays loading a blank page. No error is output on the console. When a route hangs, I can't access it anymore, BUT, I can access other routes. The fact that its totally random make it difficult to find the source of the problem.

Is there a brutal way to find what is causing the issue for sure ? I would at least be able to find a way to read error.

UPDATE 1:

Here is my app.js entry file:

import * as express from "express"
import * as exphbs from "express-handlebars"
import directory from "./source/apps/directory/directory_controller"
import users from "./source/apps/users/users_controller"
import * as session from "express-session"


// Non require variables
let app = express()
let port = process.env.PORT || 3002
import * as forms from "formidable"


app.use( session( { secret: 'keyboard cat' }))


app.use(function(req, res, next){
        res.locals.session = req.session
        next()
})


function logErrors(err, req, res, next) {
  console.error("middleware", err.stack )
  console.log("middleware", err)
  next(err)
}

app.use(logErrors)

/** Routes */
app.use( "/directory", directory )
app.use( "/users", users )

/** Views */
app.set( "views", __dirname + "/source/" )
app.engine( 'handlebars', exphbs( {
  defaultLayout: __dirname + "/source/views/layouts/main",
  partialsDir: "./source/apps/directory/views/partials/"
}) )
app.set( "view engine", "handlebars" )


// app.use(express.static(__dirname + '/public'));
app.listen( port, function () {
  console.log( "Server is listening on port:" + port )
})

Upvotes: 3

Views: 1007

Answers (1)

Robert Brax
Robert Brax

Reputation: 7318

Indeed, it was a Postgres client instance problem.

  1. I changed using a client instance to a client pooling. It seems that with my previous code, each time I would load a page, it would create a new DB instance and exhaust the default 10; explaining why it couldn't load an 11th page.

  2. I make sure to close my connection with done(); (very important) as the example in the documentation: https://github.com/brianc/node-postgres

I'm surprised that I didn't get any error message about that. I could have spent hours on that.

Upvotes: 1

Related Questions